help in understanding object creation and class

This is the place for queries that don't fit in any of the other categories.

help in understanding object creation and class

Postby deathmetal » Thu Apr 11, 2013 10:30 am

Hi Forum Members,

I have recently kick started programming in Python, Aptana 3, Vista

Code: Select all
class Piggy():
    '''
    classdocs
    '''

    counter=0
    balance=0
    name=""
    branch=""
    city=""
   
    def __init__(self,balance,name,branch):

        print("the value in counter is %d" %(self.counter))
        self.balance=balance
        self.name=name
        self.branch=branch
        print(self.balance)
        print(self.name)
        print(self.branch)
        print("All could have been printed has been printed")
        Piggy.counter=Piggy.counter+1
       
        print("Add in counter")
   
    def printArg(self):
        print(self.name)
        print(self.balance)
       
    def addSome(self,p=0):
        self.balance=self.balance+p

        self.printArg()
       
    def expenditureP(self,l=0):
       
        if l<(self.balance):
            self.balance=self.balance-l
        else:
            print("Sorry bud, you cannot spend as you don't have got sufficient funds")
       
        self.printArg()   
       


k=Piggy(100,"Rmesh","Powai")
lo=Piggy(900,"Suresh","Powai")

k.addSome(100)
k.expenditureP(9910)
k.city="B'lore"

if hasattr(k, "sex"):
    print("Good bus")
else:
    print("very bad")
   
print(getattr(k, "name"))

if hasattr(k, "name"):
    setattr(k, "name", "Rakesh")
    print(k.name)

print("The number of objects instantiate are %d" %(Piggy.counter))
print("print the number of counter for object k is %d" %(k.counter))
print("print the number of counter for object k is %d" %(lo.counter))



I have few doubts, I had previously learned Java. So may be I am unable to think in Python manner. Or may be I am drawing an analogy.
My question is:
Whenever piggy object is created: branch, name, counter, balance, city are instantiated for it correct? :?:
Each object has its own attributes.

Here in __init__(self), I am not passing counter value. But I am incrementing Class's counter. So that I can keep a track of how many objects have been created. So there counter's value should be zero?? (I hope I am not wrong.... )
That means: 2 should be value.

However, when I try to print object's counter they both give me 2 as their value :shock: (In code's 2nd last and 3rd last line). (Please explain why this is happening, as I have made changes to class variable Piggy.counter=Piggy.counter+1, or I have been wrong in declaring....)

Any explanation shall be of great help to me. :roll:
deathmetal
 
Posts: 6
Joined: Mon Apr 08, 2013 3:05 pm

Re: help in understanding object creation and class

Postby setrofim » Thu Apr 11, 2013 10:55 am

Nope. counter, balance, name, branch and city are class variables -- they belong to a class and not to instances of that class (sort of like static variables in java). So Piggy.counter, k.counter and lo.counter all refer to the same variable. You do not need to declare instance variables at class level, so in Python you would do
Code: Select all
class Piggy:

    # note that there is no name here
 
    def __init__(self, name):
        self.name = name

In your case, you're creating a class variable name, but then you're overriding it inside of __init__. So in your code, Piggy.name is valid (it will return an empty string) which is not what you want.

Do yourself a favor and forget everything you've learned about classes from Java and go through this. Also, ichabod801 wrote a nice set of tutorials on classes.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], snippsat and 3 guests