[Tutor] Help on class

bob gailer bgailer at gmail.com
Sat Sep 28 01:46:03 CEST 2013


On 9/27/2013 10:07 AM, bharath ks wrote:

 > Hello,
 >
Hi welcome to the tutor list.
Please post in plain text rather than tiny hard-to-read formatted text.

 > May i know why object 'c' does not prompt for employee name and 
employee id in the following code
You may - see comment below

 > i get out put as
 >
 > Enter employee name:john
 > Enter employee id:56
 > Employee name is: john
 > Employee id is: 56
 > ----------------------------------------------------
 > Employee name is: test
 > Employee id is: 1003
 > ----------------------------------------------------
 > Employee name is: john
 > Employee id is: 56
 >
 > class employee:
 >     emp_name=""
 >     emp_id=0
 >     def collect_name():
 >         return(input("Enter employee name:"))
 >
 >
 >     def collect_id():
 >         return(input("Enter employee id:"))
 >
 >     def show(self):
 >         print("Employee name is:",self.emp_name)
 >         print("Employee id is:",self.emp_id)
 >
 >     def __init__(self,name=collect_name(),id=collect_id()):

name=collect_name() and id=collect_id() are executed when the def is 
executed.
The values are saved as the default values of name and id.
That happens once, and the default values are used whenever __init__ is 
called without arguments.

 >         self.emp_name=name
 >         self.emp_id=id
 >
 >
 >
 > a=employee()
 > a.show()
 > print("----------------------------------------------------")
 > b=employee("test",1003)
 > b.show()
 > print("----------------------------------------------------")
 > c=employee()
 > c.show()

-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the Tutor mailing list