| | |
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:
Solved Threads: 0
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....
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)
class employee: name=' ' def get_details(self): name=input('Enter the employee name') print("In super class-->",name) class calc(employee): def pr(self): print("In sub class==>",employee.name) if __name__=='__main__': x=employee() x.get_details() y=calc() y.pr() RESULT: Enter the employee namegjh In super class--> gjh In sub class==>
•
•
Join Date: Sep 2009
Posts: 50
Reputation:
Solved Threads: 16
1
#2 Oct 22nd, 2009
Does this give you any insight?
Python Syntax (Toggle Plain Text)
>>> class employee(object): def __init__(self): self.name = raw_input('Enter employee name: ') >>> class calc(employee): def pr(self): print self.name >>> d = calc() Enter employee name: Bob >>> d.pr() Bob
1
#3 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.
python Syntax (Toggle Plain Text)
>>> class no_init(object): ... name = 'I have no init function' ... def print_name(self): ... name = input('Input some name: ') ... print('self.name', self.name) ... print('name', name) ... >>> ni = no_init() >>> ni.print_name() Input some name: Testing self.name I have no init function name Testing >>> ni.name 'I have no init function'
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)
>>> class yes_init(object): ... def __init__(self): ... self.name = 'I have no init function' ... def print_name(self): ... name = input('Input some name: ') ... print('self.name', self.name) ... print('name', name) ... >>> yi = yes_init() >>> yi.print_name() Input some name: Testing 2 self.name I have no init function name Testing 2 >>> yi.name = 'New name' >>> yi.print_name() Input some name: Testing 3 self.name New name name Testing 3 >>>
name as self.name . This makes it easier for a casual reader of code to identify which name is which.HTH
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' ...
Note: by convention class names are capitalized so you can trace them easier. Also, this is Python3 code!
python Syntax (Toggle Plain Text)
class Employee(object): name = ' ' def get_details(self): self.name = input('Enter the employee name ') print("In super class-->", self.name) class Calc(Employee): def pr(self): print("In sub class==>", self.name) if __name__=='__main__': y = Calc() y.get_details() y.pr() # create another instance y2 = Calc() y2.get_details() y2.pr()
Last edited by vegaseat; Oct 23rd, 2009 at 2:50 pm.
May 'the Google' be with you!
•
•
Join Date: Oct 2009
Posts: 37
Reputation:
Solved Threads: 0
0
#5 Oct 23rd, 2009
•
•
•
•
Does this give you any insight?
Python Syntax (Toggle Plain Text)
>>> class employee(object): def __init__(self): self.name = raw_input('Enter employee name: ') >>> class calc(employee): def pr(self): print self.name >>> d = calc() Enter employee name: Bob >>> d.pr() Bob
Yes, now i can understand the use of 'self'.
•
•
Join Date: Oct 2009
Posts: 37
Reputation:
Solved Threads: 0
0
#6 Oct 23rd, 2009
•
•
•
•
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:
As you can see, the name attribute is always "I have no init function" even though there is a "python Syntax (Toggle Plain Text)
>>> class no_init(object): ... name = 'I have no init function' ... def print_name(self): ... name = input('Input some name: ') ... print('self.name', self.name) ... print('name', name) ... >>> ni = no_init() >>> ni.print_name() Input some name: Testing self.name I have no init function name Testing >>> ni.name 'I have no init function'name" inside myprint_namefunction. 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 useself., whereas outside the class you use the object's instance. So in the above I usedni.nameasniis the instance of theno_initclass 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):
You see inside the init function I immediately started referring to the attributepython Syntax (Toggle Plain Text)
>>> class yes_init(object): ... def __init__(self): ... self.name = 'I have no init function' ... def print_name(self): ... name = input('Input some name: ') ... print('self.name', self.name) ... print('name', name) ... >>> yi = yes_init() >>> yi.print_name() Input some name: Testing 2 self.name I have no init function name Testing 2 >>> yi.name = 'New name' >>> yi.print_name() Input some name: Testing 3 self.name New name name Testing 3 >>>nameasself.name. This makes it easier for a casual reader of code to identify which name is which.
HTH
•
•
Join Date: Oct 2009
Posts: 37
Reputation:
Solved Threads: 0
0
#7 Oct 23rd, 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' ...
Note: by convention class names are capitalized so you can trace them easier.python Syntax (Toggle Plain Text)
class Employee(object): name = ' ' def get_details(self): self.name = input('Enter the employee name ') print("In super class-->", self.name) class Calc(Employee): def pr(self): print("In sub class==>", self.name) if __name__=='__main__': y = Calc() y.get_details() y.pr() # create another instance y2 = Calc() y2.get_details() y2.pr()
0
#9 Oct 23rd, 2009
Well static typing is like this:
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.
Python Syntax (Toggle Plain Text)
int num = 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.
![]() |
Similar Threads
- Problem with power in python 3.1 (Python)
- Inheritance Problem? (Python)
- C++ Inheritance problem with some syntax errors (C++)
- Problem with python and i dont know what to do.... (Python)
- problem in python (Python)
- Python problem (Python)
- problem in python. how to do this...?! (Python)
- hybrid inheritance prob (C++)
- weird cgi problem (Linux Servers and Apache)
Other Threads in the Python Forum
- Previous Thread: two values
- Next Thread: problem writing to a text file.
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied apache application argv beginner book change code color dictionary dynamic edit editing enter examples excel file filename float format ftp function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric output parameters parsing path port prime program programming projects py2exe pygame pyopengl pyqt python random recursion recursive redirect remote reverse rpg scrolledtext search server session simple smtp software sprite ssh statictext string strings syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






