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....

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==>

Does this give you any insight?

>>> 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
commented: Very Infomative +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.

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:

>>> 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'

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):

>>> 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
>>>

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

commented: Simply awesome +0

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' ...

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()

Note: by convention class names are capitalized so you can trace them easier. Also, this is Python3 code!

commented: Really nice effort +0

Does this give you any insight?

>>> 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

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'.

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:

>>> 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'

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):

>>> 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
>>>

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...

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' ...

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()

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.....

May i ask another ques? i think in java they use static to solve this issue... shall we use any such here????

Well static typing is like this:

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.