'missing 2 required positional arguments
My code wont work and it keeps repeating these error. "TypeError: years_of_employment() missing 2 required positional arguments: 'self' and 'current_year.
What I tried to do was create a class that inherited from superclass and try information hiding.
Here's the code.
class Person:
def __init__(self, first_name, last_name, birth_year, salary, employed_year):
self.__first_name = first_name
self.__last_name = last_name
self.__birth_year = birth_year
self.__salary = salary
self.__employed_year = employed_year
@property
def first_name(self):
return self.__first_name
@property
def last_name(self):
return self.__last_name
@property
def birth_year(self):
return self.__birth_year
@property
def salary(self):
return self.__salary
@property
def employed_year(self):
return self.__employed_year
def age(self, current_year):
return f'{current_year - self.birth_year}'
class Teacher(Person):
def __init__(self, first_name, last_name, birth_year, salary, employed_year):
super().__init__(first_name, last_name, birth_year)
self.salary = salary
self.employed_year = employed_year
def __str__(self):
return f'{self.first_name} {self.last_name} är född {self.birth_year} och tjäner {self.salary} kr/månad'
def years_of_employment(self, current_year):
return (current_year - self.employed_year) * '*'
person = Person("Jan", "Gran", 1986, 28000 , 2015)
Teacher.years_of_employment()
print(f'{person.first_name} is born {person.birth_year} and earn {person.salary} SEK/month')
print(f'Teacher {person.first_name} is {person.age(2022)} old')
print(f'Number of years of employment: {Teacher.years_of_employment(2022)}')
The answer should be this:
Jan Gran was born in 1986 and earns SEK 28,000 / month
The teacher
Jan is 36 years old
Number of years of employment:
Solution 1:[1]
Like the error says, you haven't provided a Teacher (self) or the current_year.
Create a Teacher, not a Person:
person = Teacher("Jan", "Gran", 1986, 28000 , 2015)
and then you can call its years_of_employment method, passing the current year as the argument:
print(person.years_of_employment(2022))
Note that you have a bug in Teacher.__init__ that will raise another exception when you try to create a Teacher, which is that you don't pass all the necessary parameters to Person.__init__. But since your Teacher doesn't init anything that Person doesn't, you don't need to define a Teacher.__init__ at all -- and all the stuff in Person.__init__ plus all its properties can be implemented automatically by decorating it as a dataclass and just listing all the properties:
from dataclasses import dataclass
@dataclass(frozen=True)
class Person:
first_name: str
last_name: str
birth_year: int
salary: int
employed_year: int
def age(self, current_year: int) -> str:
return f'{current_year - self.birth_year}'
class Teacher(Person):
def __str__(self) -> str:
return f'{self.first_name} {self.last_name} är född {self.birth_year} och tjäner {self.salary} kr/månad'
def years_of_employment(self, current_year: int) -> str:
return (current_year - self.employed_year) * '*'
person = Teacher("Jan", "Gran", 1986, 28000 , 2015)
print(f'{person.first_name} is born {person.birth_year} and earn {person.salary} SEK/month')
print(f'Teacher {person.first_name} is {person.age(2022)} old')
print(f'Number of years of employment: {person.years_of_employment(2022)}')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
