class Ant:
count = 0
def __init__(self):
if count == 0:
self.real = True
else:
self.real = False
count += 1
So basically what I want to achieve is I want only the first instance of this class to have the "real" attribute to be True
, and the subsequent attributes to be False
. I know right now this is going to give me unboundlocal error for count
. How do I make this work?
Change count
ToAnt.count
As count
is a class member (shared between all instances of Ant class) and does not belong to a specific instance you should use it with the prefix of the class name.
class Ant:
count = 0
def __init__(self):
if Ant.count == 0:
self.real = True
else:
self.real = False
Ant.count += 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With