Class methods - name error
-
I keep getting a name error when I call get_email(). I’ve made a few messages for my employee class and when I try to call them it says name error. Name not defined. Also any other suggestions on how to better my class.
class Employee(object): def __init__(self, first_name, last_name, department, employee_id): self.first_name = first_name self.last_name = last_name self.email = '{}.{}@company.com'.format(self.first_name, self.last_name) self.department = department self.employee_id = employee_id def __str__(self): return '{} {}'.format(self.first_name, self.last_name) def get_email(self): return self.email emp1 = Employee('mickey', 'mouse', 'EVS', '001') print(get_email(emp1))
-
class Employee(object): def __init__(self, first_name, last_name, department, employee_id): self.first_name = first_name self.last_name = last_name self.email = '{}.{}@company.com'.format(self.first_name, self.last_name) self.department = department self.employee_id = employee_id def __str__(self): return '{} {}'.format(self.first_name, self.last_name) def get_email(self): return self.email emp1 = Employee('mickey', 'mouse', 'EVS', '001') print(emp1.get_email())
-
Thank you. I knew it was something small. I was looking for more of a parentheses or a “,” or something that wasn’t capitalized. I wasn’t thinking that I was using the method wrong. Thank you. I didn’t want to spend three days on such a small mistake. I have before. Thank you
emp1.get_email()
Not
get_email(emp1)
-
Python 3.5 died yesterday so f-strings can now be used in all supported versions of CPython:
self.email = f'{self.first_name}.{self.last_name}@company.com'
and
return f'{self.first_name} {self.last_name}'
-
@resserone13 said:
I didn’t want to spend three days
On this forum you almost never wait during three days for an helping answer 😀
That's also an advantage of this (marvelous) app.
-
@ccc thanks I’m going to update this.
-
@cvp yr right. This forum is a great help. I was referring to me try to figure it out by myself.