Passing variables, creating instances, self, The mechanics and usage of classes: need explanation [closed] Passing variables, creating instances, self, The mechanics and usage of classes: need explanation [closed] python python

Passing variables, creating instances, self, The mechanics and usage of classes: need explanation [closed]


class Foo          (object):    # ^class name  #^ inherits from object    bar = "Bar" #Class attribute.    def __init__(self):        #        #^ The first variable is the class instance in methods.          #        #  This is called "self" by convention, but could be any name you want.        #^ double underscore (dunder) methods are usually special.  This one         #  gets called immediately after a new instance is created.        self.variable = "Foo" #instance attribute.        print self.variable, self.bar  #<---self.bar references class attribute        self.bar = " Bar is now Baz"   #<---self.bar is now an instance attribute        print self.variable, self.bar      def method(self, arg1, arg2):        #This method has arguments.  You would call it like this:  instance.method(1, 2)        print "in method (args):", arg1, arg2        print "in method (attributes):", self.variable, self.bara = Foo() # this calls __init__ (indirectly), output:                 # Foo bar                 # Foo  Bar is now Bazprint a.variable # Fooa.variable = "bar"a.method(1, 2) # output:               # in method (args): 1 2               # in method (attributes): bar  Bar is now BazFoo.method(a, 1, 2) #<--- Same as a.method(1, 2).  This makes it a little more explicit what the argument "self" actually is.class Bar(object):    def __init__(self, arg):        self.arg = arg        self.Foo = Foo()b = Bar(a)b.arg.variable = "something"print a.variable # somethingprint b.Foo.variable # Foo


So here is a simple example of how to use classes:Suppose you are a finance institute. You want your customer's accounts to be managed by a computer. So you need to model those accounts. That is where classes come in. Working with classes is called object oriented programming. With classes you model real world objects in your computer. So, what do we need to model a simple bank account? We need a variable that saves the balance and one that saves the customers name. Additionally, some methods to in- and decrease the balance. That could look like:

class bankaccount():    def __init__(self, name, money):        self.name = name        self.money = money    def earn_money(self, amount):        self.money += amount    def withdraw_money(self, amount):        self.money -= amount    def show_balance(self):        print self.money

Now you have an abstract model of a simple account and its mechanism.The def __init__(self, name, money) is the classes' constructor. It builds up the object in memory. If you now want to open a new account you have to make an instance of your class. In order to do that, you have to call the constructor and pass the needed parameters. In Python a constructor is called by the classes's name:

spidermans_account = bankaccount("SpiderMan", 1000)

If Spiderman wants to buy M.J. a new ring he has to withdraw some money. He would call the withdraw method on his account:

spidermans_account.withdraw_money(100)

If he wants to see the balance he calls:

spidermans_account.show_balance()

The whole thing about classes is to model objects, their attributes and mechanisms. To create an object, instantiate it like in the example. Values are passed to classes with getter and setter methods like `earn_money()ยด. Those methods access your objects variables. If you want your class to store another object you have to define a variable for that object in the constructor.


The whole point of a class is that you create an instance, and that instance encapsulates a set of data. So it's wrong to say that your variables are global within the scope of the class: say rather that an instance holds attributes, and that instance can refer to its own attributes in any of its code (via self.whatever). Similarly, any other code given an instance can use that instance to access the instance's attributes - ie instance.whatever.