Thursday, 5 September 2013

Understanding 'self' in Python using an example of code

Understanding 'self' in Python using an example of code

class Bank(): # let's create a bank, building ATMs
crisis = False
def create_atm(self):
while not self.crisis:
yield "$100"
x=bank():
I'm new to python (and coding in general) and I'm trying to wrap my head
around self. I don't know if this is the best example to try to learn
'self', but I don't understand the line "while not self.crisis: yield
$100" So, "crisis=false" so does the 'create atm' function basically say
"while crisis = true yield $100?" Still not sure what self is doing.
Also, why is there a colon after x=bank()?

No comments:

Post a Comment