0

I can't get the return value True|False from a function. What Am I doing wrong?

The script looks roughly like that.

class getLogs:

    IAM=getpass.getuser()
    SRC_LOG_DIR='/server/log/session/'
    DST_LOG_DIR="/home/${IAM}/LOGS/"

    def __init__(self):

        parser = argparse.ArgumentParser()
        parser.add_argument("-S","--sessId", type=int, help="The SessionId of the logs to retreive for.")
        args = parser.parse_args()



        if not args.sessId:
            args.sessId = 'ns'
            os.system('clear')
            print ('\tNo sessid was set.')
            print ('\tTip: you can see what options you have by running "{} -h"'.format(__file__))            

        self.getSessID(args.sessId)

        if self.getSessID:
            print('\tI will get the logfiles for session ID "{}".'.format(self.sessId))            
            #Do something else

    def getSessID(self, sessId):

        if sessId == 'ns':
            menu='on'        
            while menu=='on':

                sys.stdout.write("\tPlease give me the sessionid of the logging :")
                sessId = raw_input()
                test = re.match("^[0-9]{5,7}$",sessId)

                if test:
                    menu='off'


                if not test:
                    print('\tError:\n\tSessionID should contain only numbers')
                    print('\tor sessID was not defined.')
                    print('\tor sessID length must be 5 to 7 characters')

        self.sessId = sessId
        return True

And what I get from this print self.getSessID and from if self.getSessID in the constructor function is this <bound method getLogs.getSessID of <__main__.getLogs instance at 0x7f5aa28e1518>>

Im sure I am doing something wrong, I haven't understood how to get the value from the return in a function. Could someone please help me?

4

1 回答 1

1

您没有对该方法执行任何操作,然后测试该方法是否存在:

self.getSessID(args.sessId)

if self.getSessID:
    print('\tI will get the logfiles for session ID "{}".'.format(self.sessId))            

您需要存储结果并测试

session_id = self.getSessID(args.sessId)

if session_id:
    print('\tI will get the logfiles for session ID "{}".'.format(session_id))     

也可能是您想self.sessId在方法中设置的test self.getSessID()

于 2013-06-05T20:10:35.387 回答