I have recently kick started programming in Python, Aptana 3, Vista
- Code: Select all
print("the value in counter is %d" %(self.counter))
print("All could have been printed has been printed")
print("Add in counter")
print("Sorry bud, you cannot spend as you don't have got sufficient funds")
if hasattr(k, "sex"):
if hasattr(k, "name"):
setattr(k, "name", "Rakesh")
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....)
Any explanation shall be of great help to me.