Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
self in functions
#1
I created a class:

class AIHome:

def __init__(self):
     self.fetchUpdate();

def fetchUpdate(self):
     self.relayToggle()

def relayToggle(self):
     self.write(self,1,1)

def write(self, relay, state):
     ...do this and that
I havent used any methods until now that I need to send a relay value and state value.  So how should I call that method properly.
Reply
#2
First all your methods (if in a class, not functions, rather methods) need to be indented.
The calling sequence would be:
AIHome.write(relay, state)
unless called from another method that is part of the same class, in that case, the method
would be called like:
self.write(relay, state)
Reply
#3
Ok, yeah I meant that when I call a method from another method, the method must be defined as:

def someMethod(self, p1, p2)...

but when called I ignore the self in the method signature, right? So I just do:

self.someMethod(p1,p2)

without the self parameter.
Reply
#4
correct
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020