Reputation: 3667
pretty new to Python - OO - have been using the procedural python for some time. I wrote a silly class and method out and am having trouble invoking my method:
class Jpd:
#attributes share data among class objects.
my_age = 26
def __init__(self, value):
#method-level attribute data.
self.my_age = value
def ageToRetireFrom(self ):
print self.my_age + 20
def ageToBuyHome(self):
print self.my_age + 5
retire = Jpd.ageToRetireFrom()
home = Jpd.ageToBuyHome()
If I try calling my file example.py
like:
>>> import example
>>> j = example.Jpd()
I get:
NameError: global name 'value' is not defined
I know this is complaining that my value
attribute isn't assigned. Can someone help me invoke this method?
Thank you
Upvotes: 0
Views: 62
Reputation: 10951
when you create an instance of your class Jpd
, should be this way:
j = example.Jpd(20)
since you defined your __init__(self, value)
, so you need to pass a value when creating an instance of your class
Upvotes: 1
Reputation: 47988
When you define the __init__
method, that becomes the call signature when you want to create a new instance. So from your example, you'd use:
j = example.Jpd(15)
To make a new Person who was 15 years old.
Upvotes: 1