944,077 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 605
  • Python RSS
Oct 22nd, 2009
0

inheritance problem in python. help please.

Expand Post »
though i'm calling pr() function in the subclass after calling the get_details() function(which assigns value to name) it doesn't print the current value of name.
My question is if i want to print the value of name in the subclass what should i do???? I've tried in many ways using __init__ and blah..blah.. so please help me out....
Python Syntax (Toggle Plain Text)
  1. class employee:
  2. name=' '
  3. def get_details(self):
  4. name=input('Enter the employee name')
  5. print("In super class-->",name)
  6.  
  7. class calc(employee):
  8. def pr(self):
  9. print("In sub class==>",employee.name)
  10.  
  11. if __name__=='__main__':
  12. x=employee()
  13. x.get_details()
  14. y=calc()
  15. y.pr()
  16.  
  17.  
  18.  
  19. RESULT:
  20.  
  21. Enter the employee namegjh
  22. In super class--> gjh
  23. In sub class==>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
python.noob is offline Offline
37 posts
since Oct 2009
Oct 22nd, 2009
1
Re: inheritance problem in python. help please.
Does this give you any insight?

Python Syntax (Toggle Plain Text)
  1. >>> class employee(object):
  2. def __init__(self):
  3. self.name = raw_input('Enter employee name: ')
  4.  
  5.  
  6. >>> class calc(employee):
  7. def pr(self):
  8. print self.name
  9.  
  10.  
  11. >>> d = calc()
  12. Enter employee name: Bob
  13. >>> d.pr()
  14. Bob
Reputation Points: 14
Solved Threads: 16
Junior Poster in Training
lukerobi is offline Offline
50 posts
since Sep 2009
Oct 22nd, 2009
1
Re: inheritance problem in python. help please.
though i'm calling pr() function in the subclass after calling the get_details() function(which assigns value to name) it doesn't print the current value of name.
Actually, it is printing the current value of name. The employee.name is always a space (' '), because you never change it. In your get_details function, you have created a new variable called name and assigned the users' input to it. Why don't you try printing x.name after your call to x.get_details()? You'll see that x.name is still just a blank space. The problem is that then name object that you're creating inside get_details only exists inside the scope of that function. You should be using self.name, which is the persistent attribute of the employee class. Let me demonstrate this principle:
python Syntax (Toggle Plain Text)
  1. >>> class no_init(object):
  2. ... name = 'I have no init function'
  3. ... def print_name(self):
  4. ... name = input('Input some name: ')
  5. ... print('self.name', self.name)
  6. ... print('name', name)
  7. ...
  8. >>> ni = no_init()
  9. >>> ni.print_name()
  10. Input some name: Testing
  11. self.name I have no init function
  12. name Testing
  13. >>> ni.name
  14. 'I have no init function'
As you can see, the name attribute is always "I have no init function" even though there is a " name " inside my print_name function. This is because that particular object is created and destroyed inside that function only. To refer to an attribute of a class from inside the class declaration you need to use self. , whereas outside the class you use the object's instance. So in the above I used ni.name as ni is the instance of the no_init class and name is the attribute I'm looking for.

The following code is identical but uses an init function instead (this is the recommended way to do classes, and it's good to get into the habit of using them):
python Syntax (Toggle Plain Text)
  1. >>> class yes_init(object):
  2. ... def __init__(self):
  3. ... self.name = 'I have no init function'
  4. ... def print_name(self):
  5. ... name = input('Input some name: ')
  6. ... print('self.name', self.name)
  7. ... print('name', name)
  8. ...
  9. >>> yi = yes_init()
  10. >>> yi.print_name()
  11. Input some name: Testing 2
  12. self.name I have no init function
  13. name Testing 2
  14. >>> yi.name = 'New name'
  15. >>> yi.print_name()
  16. Input some name: Testing 3
  17. self.name New name
  18. name Testing 3
  19. >>>
You see inside the init function I immediately started referring to the attribute name as self.name . This makes it easier for a casual reader of code to identify which name is which.

HTH
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Oct 22nd, 2009
1
Re: inheritance problem in python. help please.
Since Employee is inherited by Calc it also inherits method get_details(). So use this method with the instance of Calc. You need to keep track of the instance with 'self' ...
python Syntax (Toggle Plain Text)
  1. class Employee(object):
  2. name = ' '
  3. def get_details(self):
  4. self.name = input('Enter the employee name ')
  5. print("In super class-->", self.name)
  6.  
  7.  
  8. class Calc(Employee):
  9. def pr(self):
  10. print("In sub class==>", self.name)
  11.  
  12.  
  13. if __name__=='__main__':
  14. y = Calc()
  15. y.get_details()
  16. y.pr()
  17. # create another instance
  18. y2 = Calc()
  19. y2.get_details()
  20. y2.pr()
Note: by convention class names are capitalized so you can trace them easier. Also, this is Python3 code!
Last edited by vegaseat; Oct 23rd, 2009 at 2:50 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 23rd, 2009
0
Re: inheritance problem in python. help please.
Click to Expand / Collapse  Quote originally posted by lukerobi ...
Does this give you any insight?

Python Syntax (Toggle Plain Text)
  1. >>> class employee(object):
  2. def __init__(self):
  3. self.name = raw_input('Enter employee name: ')
  4.  
  5.  
  6. >>> class calc(employee):
  7. def pr(self):
  8. print self.name
  9.  
  10.  
  11. >>> d = calc()
  12. Enter employee name: Bob
  13. >>> d.pr()
  14. Bob
Thanks lukerobi. Actually i've joined this community yesterday only. I never thought i would get 3 awesome replies within a day of asking the ques..
Yes, now i can understand the use of 'self'.
Reputation Points: 10
Solved Threads: 0
Light Poster
python.noob is offline Offline
37 posts
since Oct 2009
Oct 23rd, 2009
0
Re: inheritance problem in python. help please.
Click to Expand / Collapse  Quote originally posted by jlm699 ...
Actually, it is printing the current value of name. The employee.name is always a space (' '), because you never change it. In your get_details function, you have created a new variable called name and assigned the users' input to it. Why don't you try printing x.name after your call to x.get_details()? You'll see that x.name is still just a blank space. The problem is that then name object that you're creating inside get_details only exists inside the scope of that function. You should be using self.name, which is the persistent attribute of the employee class. Let me demonstrate this principle:
python Syntax (Toggle Plain Text)
  1. >>> class no_init(object):
  2. ... name = 'I have no init function'
  3. ... def print_name(self):
  4. ... name = input('Input some name: ')
  5. ... print('self.name', self.name)
  6. ... print('name', name)
  7. ...
  8. >>> ni = no_init()
  9. >>> ni.print_name()
  10. Input some name: Testing
  11. self.name I have no init function
  12. name Testing
  13. >>> ni.name
  14. 'I have no init function'
As you can see, the name attribute is always "I have no init function" even though there is a " name " inside my print_name function. This is because that particular object is created and destroyed inside that function only. To refer to an attribute of a class from inside the class declaration you need to use self. , whereas outside the class you use the object's instance. So in the above I used ni.name as ni is the instance of the no_init class and name is the attribute I'm looking for.

The following code is identical but uses an init function instead (this is the recommended way to do classes, and it's good to get into the habit of using them):
python Syntax (Toggle Plain Text)
  1. >>> class yes_init(object):
  2. ... def __init__(self):
  3. ... self.name = 'I have no init function'
  4. ... def print_name(self):
  5. ... name = input('Input some name: ')
  6. ... print('self.name', self.name)
  7. ... print('name', name)
  8. ...
  9. >>> yi = yes_init()
  10. >>> yi.print_name()
  11. Input some name: Testing 2
  12. self.name I have no init function
  13. name Testing 2
  14. >>> yi.name = 'New name'
  15. >>> yi.print_name()
  16. Input some name: Testing 3
  17. self.name New name
  18. name Testing 3
  19. >>>
You see inside the init function I immediately started referring to the attribute name as self.name . This makes it easier for a casual reader of code to identify which name is which.

HTH
thanks jlm699.. thank you so much for your clearcut explanation.. it'll be not only to me (who is new to python lang.) but 'll be useful to everybody who are going to read this in the time to come...
Reputation Points: 10
Solved Threads: 0
Light Poster
python.noob is offline Offline
37 posts
since Oct 2009
Oct 23rd, 2009
0
Re: inheritance problem in python. help please.
Click to Expand / Collapse  Quote originally posted by vegaseat ...
Since Employee is inherited by Calc it also inherits method get_details(). So use this method with the instance of Calc. You need to keep track of the instance with 'self' ...
python Syntax (Toggle Plain Text)
  1. class Employee(object):
  2. name = ' '
  3. def get_details(self):
  4. self.name = input('Enter the employee name ')
  5. print("In super class-->", self.name)
  6.  
  7.  
  8. class Calc(Employee):
  9. def pr(self):
  10. print("In sub class==>", self.name)
  11.  
  12.  
  13. if __name__=='__main__':
  14. y = Calc()
  15. y.get_details()
  16. y.pr()
  17. # create another instance
  18. y2 = Calc()
  19. y2.get_details()
  20. y2.pr()
Note: by convention class names are capitalized so you can trace them easier.
Thanks vegaseat.. I got the answer... Really you guys are awesome... This community rocks... Added reps to all of you.....
Reputation Points: 10
Solved Threads: 0
Light Poster
python.noob is offline Offline
37 posts
since Oct 2009
Oct 23rd, 2009
0
Re: inheritance problem in python. help please.
May i ask another ques? i think in java they use static to solve this issue... shall we use any such here????
Reputation Points: 10
Solved Threads: 0
Light Poster
python.noob is offline Offline
37 posts
since Oct 2009
Oct 23rd, 2009
0
Re: inheritance problem in python. help please.
Well static typing is like this:

Python Syntax (Toggle Plain Text)
  1. int num = 2
  2. str var1 = "a"

Python is dynamically typed meaning you don't have to type int, str, or that kind of thing before a variable. Or are you thinking of something else.
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: two values
Next Thread in Python Forum Timeline: problem writing to a text file.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC