oop - python class variable in static method -
[updated]: full code
i confused pyhton's staticmethods according this (last answer), should work!
getting error:
attributeerror: class myconnection has no attribute 'myuser'
class myconnection: def __init__(self, hostname, port, user, password): myhostname = hostname myport = port myuser = user mypassword = password isisessid = none @staticmethod def connect(): my_session = myconnection() headers = {'content-type': 'application/json'} headers['authorization'] = 'basic ' + string.strip( base64.encodestring(myconnection.myuser + ':' + myconnection.mypassword)) body = json.dumps({'username': myconnection.myuser, 'password': myconnection.mypassword, 'services': ['platform', 'namespace']}) uri = '/session/1/session' connection = httplib.httpsconnection(myconnection.myhostname, myconnection.myport) connection.connect() try: connection.request('post', uri, body, headers) response = connection.getresponse() my_session.isisessid = myconnection.extract_session_id( response.getheaders()) except exception, e: print e connection.close() except httplib.badstatusline, e: print e connection.close() return my_session
if attributes going static, don't initialize them in initializer method, declare them outside @ class level, not @ method level.
but why initializing class attributes in initializer? every instance create overwrite values!
i believe you're confusing instance attributes , class attributes used for. why don't try using instance attributes? things considered, having static data not idea. example:
class myconnection: def __init__(self, hostname, port, user, password): self.myhostname = hostname self.myport = port self.myuser = user self.mypassword = password @staticmethod def connect(): my_session = myconnection() print my_session.myuser # example
Comments
Post a Comment