0

I am just Practising for Classes in python and stuck here Here, My code:

class Employee():
          def __init__(self,name):
               self.name = name

          def name(self):
               print(self.name)
e1 = Employee('Prashant')   #I m gonna to use them as an id of Employee
e2 = Employee('Vishal')
e3 = Employee('Harry')

a = input('Enter your Employee id')  #I am gonna use this as an argument

Employee.nameandage(a) #OR
a.name()

And here I Got my error as:-

AttributeError: 'str' object has no attribute 'first'

Simply I just want to ask that if I can use str as an argument or not.....

3
  • 2
    Where is first used? Commented Oct 13, 2019 at 12:54
  • What is this supossed to do? Employee.nameandage(a) Commented Oct 13, 2019 at 12:57
  • 3
    @Prashant please provide code which is relate to the mistake you did not use first as an attribute in the peace of code you have shown. In addiition you use static method nameandage where was the method defined? Plus you have a conflict in your code, you have method name and a property name. Commented Oct 13, 2019 at 12:57

2 Answers 2

2

Firstly, python interpreter treats 'name' as the variable 'name' that is a string other than the method name(). Just change the name of the function from "name()" to "get_name()" and it will work.

Secondly, you are calling the name method from the input str, not from the object.

a = input('Enter your Employee id')
...
a.name()

Here is a rewritten version of your code (I don't know what you want to do with the id, but you can change this code to do exactly what you want). I hope it will help you

class Employee:
    def __init__(self, name):
        self.name = name

    def get_name(self):
        print(self.name)

e1 = Employee('Prashant')
e2 = Employee('Vishal')
e3 = Employee('Harry')


a = input('Enter your Employee Name: ')

e1.name = a
# OR
e4 = Employee(a)

# both print the employee name you entered as input, stored in a
e1.get_name() 
e4.get_name()
Sign up to request clarification or add additional context in comments.

Comments

0

It is not so much using a string as an argument. When you are setting attributes of a class you need to reference them in your code. Take this example:

class Employee:
    def __init__(self, name, age):
        self.name = name
        self.age = age


employee_1 = Employee 

add_name = input('Enter name:> ')
add_age = int(input('Enter age:> '))
employee_1.name = add_name 
employee_1.age = add_age   

print('Employee name: ', employee_1.name, '\nEmployee age: ', employee_1.age)

Output:

Enter name:> John Smith
Enter age:> 40
Employee name:  John Smith 
Employee age:  40

You do not have to use an input to set the information. You can simply set it from the variable employee_1.

employee_1 = Employee('John Smith', 40)

There is a method using super() where you can create a subclass with extra attributes that wont affect the Parent class.

class Person1(Employee):  # passing the parent class through

    def __init__(self):
        self.email = '[email protected]'
        super().__init__(name='John Smith', age=40)


employee = Person1()
employee_1 = Employee('John Doe', 20)
print('Employee name: ', employee.name, '\nEmployee age: ', employee.age, '\nEmployee email: ', employee.email)
print('Employee name: ', employee_1.name, '\nEmployee age: ', employee_1.age)

Output:

Employee name:  John Smith 
Employee age:  40 
Employee email:  [email protected]
Employee name:  John Doe 
Employee age:  20

As you can see I could add the attribute email without the Parent Class being affected however the attributes from the Parent Class were inherited.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.