python - self argument not defined when calling method? -


hi developing system making use of phidget sensors , having issues trying write variables database. have read around fair amount on classes , calling variables cannot code run.

my code below, can see have put 'self' argument when database method called complaining 'self' not defined. if don't have self in there complaint 'database() missing 1 required positional argument: 'self''

connection = sqlite3.connect('testing.db')       cursor = connection.cursor()  class sensors():      def interfacekitattached(self, e):         attached = e.device         print("interfacekit %i attached!" % (attached.getserialnum()))      def sensorinputs(self, e):            temperature = (interfacekit.getsensorvalue(0)*0.2222 - 61.111)         self.sound =  (16.801 * math.log((interfacekit.getsensorvalue(1))+ 9.872))         light = (interfacekit.getsensorvalue(2))          print("temperature is: %i " % temperature)         print("sound : %i" %self.sound)         print("light is: %i \n" %light)          interfacekit.setsensorchangetrigger(0, 25)         interfacekit.setsensorchangetrigger(1, 35)         interfacekit.setsensorchangetrigger(2, 60)       def database(self):           cursor.execute('''create table events1         (sensor text, value text)''')          cursor.execute ('insert events1 values (?, ?)', ('sounding', self.sound))           connection.commit()         connection.close()     try:     interfacekit.setonattachhandler(interfacekitattached)     interfacekit.setonsensorchangehandler(sensorinputs)     database(self) 

thanks

self not argument pass in parenthesis. self argument passed follows:

object.method()

here passing object argument, self.

therefore, in case, need call method in form of object.method() object instance of class method in. in case, class sensors.

so not do:

database(self)

you

sensors_object = sensors()

sensors_object.database()

when call sensors_object = sensors() call, create new sensors object. call object's method not class's method in line follows.

[note:] recommend create def __init__(self): method. method gets called when create new instance of class. in __init__ method, initialize different variables need self.variable_name = something


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -