Python Forum
self in functions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: self in functions (/thread-1596.html)



self in functions - marciokoko - Jan-15-2017

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.


RE: self in functions - Larz60+ - Jan-15-2017

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)



RE: self in functions - marciokoko - Jan-17-2017

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.


RE: self in functions - Larz60+ - Jan-17-2017

correct