inheritance problem in python. help please.

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 37
Reputation: python.noob is an unknown quantity at this point 
Solved Threads: 0
python.noob python.noob is offline Offline
Light Poster

inheritance problem in python. help please.

 
0
  #1
Oct 22nd, 2009
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....
  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==>
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 50
Reputation: lukerobi is an unknown quantity at this point 
Solved Threads: 16
lukerobi lukerobi is offline Offline
Junior Poster in Training
 
1
  #2
Oct 22nd, 2009
Does this give you any insight?

  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
1
  #3
Oct 22nd, 2009
Originally Posted by python.noob View 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.
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:
  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):
  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
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,113
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 944
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
1
  #4
Oct 22nd, 2009
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' ...
  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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 37
Reputation: python.noob is an unknown quantity at this point 
Solved Threads: 0
python.noob python.noob is offline Offline
Light Poster
 
0
  #5
Oct 23rd, 2009
Originally Posted by lukerobi View Post
Does this give you any insight?

  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'.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 37
Reputation: python.noob is an unknown quantity at this point 
Solved Threads: 0
python.noob python.noob is offline Offline
Light Poster
 
0
  #6
Oct 23rd, 2009
Originally Posted by jlm699 View Post
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:
  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):
  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...
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 37
Reputation: python.noob is an unknown quantity at this point 
Solved Threads: 0
python.noob python.noob is offline Offline
Light Poster
 
0
  #7
Oct 23rd, 2009
Originally Posted by vegaseat View Post
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' ...
  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.....
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 37
Reputation: python.noob is an unknown quantity at this point 
Solved Threads: 0
python.noob python.noob is offline Offline
Light Poster
 
0
  #8
Oct 23rd, 2009
May i ask another ques? i think in java they use static to solve this issue... shall we use any such here????
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster
 
0
  #9
Oct 23rd, 2009
Well static typing is like this:

  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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC