Question

I read somewhere that __init__ stores information while creating the object. So, let's say I have this code:

class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit 

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount 

myAccount = BankAccount(16.20)

x = raw_input("What would you like to do?")

if x == "deposit":    
    myAccount.deposit(int(float(raw_input("How much would you like to deposit?"))))
    print "Total balance is: ", myAccount.balance()   
elif x == "withdraw":    
    myAccount.withdraw(int(float(raw_input("How much would you like to withdraw?"))))
    print "Total balance is: ", myAccount.balance()    
else:
    print "Please choose 'withdraw' or 'deposit'. Thank you."

What is __init__ doing or storing. I don't understand what "self.amount" is or how making it = deposit does anything. Is the "self.amount" under __init__ the same as the one under withdraw? I'm just not understanding what "self_amount" does.

Was it helpful?

Solution

Q What is __init__ doing or storing?

A __init__ gets called whenever you construct an instance of the class. This applies to all classes. It is customary to initialize all your data in this function. In your particular case, you are creating a member data called amount and assigning it to be the same as the deposit argument passed to the function.

Q I don't understand what "self.amount" is or how making it = deposit does anything.

A The statement self.amount = deposit accomplishes couple of things. It creates a member data of the class named amount and assigns it to be the value of deposit.

Q Is the "self.amount" under __init__ the same as the one under withdraw?

A Yes.

Q I'm just not understanding what "self.amount" does.

A It allows you to capture the data of the object. Every class needs to figure out member data it needs to work correctly. In your case, the only data you need is the amount. If you had a class called Employee, it might look something like:

class Employee(object):
    def __init__(self, firstName, lastName, id, salary):
        self.firstName = firstName 
        self.lastName = lastName 
        self.id = id 
        self.salary = salary 

OTHER TIPS

In Python, __init__() is a method called when a instance of the class is created. This method is usually used to initialize instance variables. After initializing, you can use them in other methods.

About self.amount, it's an instance variable (there are class variables too). In other words an attribute.

Your class is BankAccount, each bank account should have an amount. That's when instance variables become helpful!

When you create an instance

myAccount = BankAccount(16.20)

the __init__() method is called, (you can see this by putting a print inside), and it will set self.amount with 16.20.

From your question I think you are new to object-oriented programming (OOP). I suggest reading about it, and how it is implemented in Python. Here is a short starting point: http://www.tutorialspoint.com/python/python_classes_objects.htm

Then feel free to search about the new concepts that you learned there.

In short, to get directly to the point of your question:

  • Classes are object templates;
  • An object that implements such a template is called a instance of the class;
  • Objects may have instance variables, which store a value specific to each instance;
  • Classes may define a constructor, called when creating a instance of the class. These constructor are usually used to initialize the instance variables.
  • In Python, all the methods of a class take the instance they work on as an argument, named self by convention
  • In Python, __init__ is the name of the constructor

So in Python when you call myAccount.deposit(10), you could say it is equivalent to BankAccount.deposit(myAccount, 10). So yes, the self variable is "the same" in all your four methods, and in your example it is assigned with myAccount.

You probably confusing self.amount with amount. self.amount is a variable associated with class BankAccount, it gets initialized when the instance of the class is created. So, self.amount is same everywhere in your class, but amount is an argument, so it depends on the value you give at function calling. Read : http://docs.python.org/2/tutorial/classes.html and http://www.voidspace.org.uk/python/articles/OOP.shtml#the-init-method

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top