-4
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, since 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()?

icedwater
  • 4,472
  • 3
  • 33
  • 47
david
  • 5,443
  • 13
  • 44
  • 82

4 Answers4

0
x = bank():

is improper code and clearly gives you an error when running the code

And the usage of ''self'' is answered here:

Explaining the python 'self' variable to a beginner

Exactly in the same question where you took your code from.

Community
  • 1
  • 1
0

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,

This SO article explains 'self' well as noted in the comment. Please read through it.

Also, why is there a colon after x=bank()?

That is incorrect, and not just because of the colon. You should also use x = Bank() (note the capital), and this declaration should be outside the class most likely elsewhere in your code, like the main routine.

So, since crisis = False so does the create_atm function basically say while crisis = true yield $100?

No, that will be yielded only if the crisis is False, hence the not. To understand it easily, if crisis would mean that if crisis is not false, then do this and that. Now, you have got a negate in there, so do this and that if it is false.

Community
  • 1
  • 1
lpapp
  • 48,739
  • 39
  • 106
  • 133
0

As far as the last colon is concerned, I'm not sure why x = bank(): is written that way. As has been suggested, it's probably a mistake.

In the example, if x = Bank() is executed, x is an instance of the Bank class, or a Bank object. This object has a copy of the function that is defined below, which all Bank objects should have:

def create_atm(self):
    while not self.crisis:
        yield "$100"

The line before the function defines crisis as being False. Here crisis is an internal variable or a property which by default has a value False. This is important to answer your next question:

So, since crisis = False so does the create_atm function basically say while crisis = true yield $100?

while statements are only executed when the condition tested is True. In this case the condition is not self.crisis, the opposite of whatever is in self.crisis. So you need to look at the value of crisis that this Bank sees in itself, and negate that.

If self.crisis is True then not self.crisis would be False and the while loop will not even execute.

I guess you could say self is a parameter passed to the function so that it can access its own variables. That way, one Bank could be in crisis while another is not.

icedwater
  • 4,472
  • 3
  • 33
  • 47
0

A little bit of reading OOP will help to understand self. If you notice self is the first argument. It does not have to be self it is just traditional name that used for first argument. And what it is is a reference to the instantiated object - "luck".

Now we can assign and use variable that is encapsulated within that object and that is actually carried around. So if we had different instances of the same class, they would have separate variable, separate value in self.sum. This init function is special. This is called a Constructor.

So "luck" gets assigned Bank, and that creates an object called "luck", which is an instance of Bank. As that object is created, init is called, and init is called with a reference to self and with variable 0.

I hope that helps.

#!/usr/bin/env python
# encoding: utf-8

class Bank(): # let's create a bank, building ATMs

    def __init__(self, sum):
        self.sum = sum

    def create_atm(self):
        if self:
           self.sum = 100
        return self.sum

luck = Bank(0)

print luck.create_atm()
Askar
  • 473
  • 3
  • 19