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 (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....)

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