__init__() missing 1 required positional argument (Python3) [duplicate] - python

This question already has answers here:
__init__() missing 1 required positional argument
(6 answers)
Closed 4 years ago.
during my python learning I've got an error in this code:
class User:
def __init__(self, first_name, last_name, location, payment_method):
self.first_name = first_name
self.last_name = last_name
self.location = location
self.payment_method = payment_method
self.login_attempts = 0
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
def printing_login_attempts(self):
print('Quantity of login attempts: ' + str(self.login_attempts))
def describe_user(self):
print('Current user first name: ' + self.first_name.title())
print('Current user last name: ' + self.last_name.title())
print('Location is ' + self.location.title() + '. Payment method is ' + self.payment_method)
def greet_user(self):
print('Hello ' + self.first_name.title() + ' ' + self.last_name.title() + '!!!')
class Admin(User):
def __init__(self, first_name, last_name, location, payment_method):
super().__init__(first_name, last_name, location, payment_method)
self.privilegis = Privilegis()
class Privilegis:
def __init__(self, privilegis_type):
self.privilegis_type = ['Allow to delete users', 'Allow to rename users', 'Allow to ban users']
def show_privilegis(self):
print('Special admins privilegis are: ' + ', '.join(self.privilegis))
new_user = Admin('John', 'Edwards', 'LA', 'visa')
new_user.privilegis.show_privilegis()
error description
self.privilegis = Privilegis()
TypeError: __init__() missing 1 required positional argument: 'privilegis_type'
I cant get what the problem is. How should I use one class(Privilegis) as attribute of another(Admin) ?

In your Privileges class, you write:
class Privileges:
def __init__(self, privilegis_type):
self.privilegis_type = ['Allow to delete users', 'Allow to rename users', 'Allow to ban users']
# ...
You thus specify that it requires a privilegis_type parameter, but that is strange, since later, you do nothing with that.
Either you should probably rewrite the constructor, and handle the parameter (and thus provide one when you construct a Privilegis object, or you should get rid of the parameter.

Your Admin class has a privilegis attribute that initializes an instance of Privilegis. The __init__() method defines how you initialize the class, and in your case it requires a privilegis_type as a parameter.
If you want this privilege to pass to the Admin at instantiation time, add the parameter to its __init__ method and pass that internally to the privilege attribute.
class Admin(User):
def __init__(self, first_name, last_name, location, payment_method, privilegis_type): # add privilegis type
super().__init__(first_name, last_name, location, payment_method)
self.privilegis = Privilegis(privilegis_type) # pass that privilegis type, needed by the Privilegis class
class Privilegis:
def __init__(self, privilegis_type):
self.privilegis_type = ['Allow to delete users', 'Allow to rename users', 'Allow to ban users']
def show_privilegis(self):
print('Special admins privilegis are: ' + ', '.join(self.privilegis_type)) # fixed that also
although in your code there are two major errors:
the Privilegis class is not using the parameter at all. You could maintain your original code, and remove the privilegis_type or use a sane default, which will depend on what you are trying to do.
self.privilegis is not defined anywhere inside Privilegis. I believe you want it to be self.privilegis_type
this means having something like this:
class Privilegis:
def __init__(self, privilegis_type=None): # could be anything
self.privilegis_type = ['Allow to delete users', 'Allow to rename users', 'Allow to ban users']
def show_privilegis(self):
print('Special admins privilegis are: ' + ', '.join(self.privilegis_type)) # fixed that also

Related

Problems with multiplying a class variable with an instance

I am new to pyhton and also new to working with classes. I am working on the below problem where I want to multiply the class variable (raise_amount) by the instance salary. However, when i do this, I get None as output. I would like to get the salary amount per person multiplied by 1.04. Any help will be greatly appreciated.
class Person:
raise_amount = 1.04
def __init__(self, name, street_name, house_nr, post_code, salary): #:post_code, salary):
self.name = name
self.street_name = street_name
self.house_nr = house_nr
self.post_code = post_code
self.salary = salary
def street_name_and_house_nr(self):
return '{} {}'.format(self.street_name, self.house_nr)
def apply_raise(self): # here is the code that seems to have problems
self.salary = int(Person.raise_amount * self.salary)
def street_name_and_house_nr_salary(self):
return self.name + ' ' + str(self.salary)
prs_1 = Person("Mary's", 'Broadway', 304, '2526 CG', 10)
prs_2 = Person("Jhon's", 'Longstreet', 304, '2829 AK',7)
prs_3 = Person("Larry's", 'Chinstreet', 58, '3046 JP', 8)
print(Person.apply_raise(prs_1))
print(Person.apply_raise(prs_2))
print(Person.apply_raise(prs_3))
This is the output i get when i run the code
None
None
None
apply_raise() doesn't return the new salary, it just updates the salary attribute. So you should get that separately to print it.
prs_1.apply_raise()
print(prs_1.salary)
Other notes:
Conventionally the first argument to methods is self. Don't make up your own name (what does lelf mean?).
You should call methods using instance.method(), not Class.method(instance). This ensures that the proper method will be used when the instance is in a subclass.

Python Error : TypeError: patientInfo() missing 1 required positional argument: 'Disease'

I'm brand new to Python, and I'm trying to learn how to work with classes. Does anyone know how come this is not working?
code is here:
this is my patient class
class Patient:
# Constructor
def __init__(self, name, Age, Gender, Disease):
self.name = name
self.Age = Age
self.Gender = Gender
self.Disease = Disease
# Function to create and append new patient
def patientInfo(self, Name, Age, Gender, Disease):
# use ' int(input()) ' method to take input from user
ob = Patient(Name, Age, Gender, Disease)
ls.append(ob)
and want to access patientInfo in main class but while accessing getting errors regarding the positional argument.
code of the main class is here:
elif user_choice == 2:
print()
f = open("myfile.txt", "a")
f.write(input('\n'))
# "{}\n".format(name)
name = f.write(input("Name: "+ "\n"))
f.write("\n")
Age = f.write((input("Age: "+ "\n")))
Gender = f.write(input("Gender: "+ "\n"))
Disease = f.write(input("Disease: "+ "\n"))
f.close()
Patient.patientInfo(name, Age, Gender, Disease)
can you please suggest to me where I am going wrong?
For your code to work, you have 2 options:
If you want to call patientInfo without creating an object of class Patient then change the function as below:
#staticmethod
def patientInfo(Name, Age, Gender, Disease):
# use ' int(input()) ' method to take input from user
ob = Patient(Name, Age, Gender, Disease)
ls.append(ob)
If you dont want to change the function declaration as above:
Create an object (say obj) of class Patient and call it as obj.patientInfo(...)

Python project help (classes/expected type)

I am working on a project for school, simulating a payroll program, and I am getting an error. The error I am getting is
'Expected type 'Classification', got 'Employee' instead'. The relevant code is (I put *** around the code generating the error, it is the 5th function under the Employee Class).
class Employee:
def __init__(self, emp_id, first_name, last_name, address, city, state, zipcode, clas = None):
self.emp_id = emp_id
self.first_name = first_name
self.last_name = last_name
self.address = address
self.city = city
self.state = state
self.zipcode = zipcode
self.classification = clas
def make_hourly(self, hourly_rate):
self.clas = Hourly(hourly_rate)
self.classification = self.clas
def make_salaried(self, salary):
self.clas = Salaried(salary)
self.classification = self.clas
def make_commissioned(self, salary, rate):
self.clas = Commissioned(rate, salary)
self.classification = self.clas
def issue_payment(self):
***pay = Classification.compute_pay(self)***
print('Mailing', pay, 'to', self.first_name, self.last_name, 'at', self.address, self.city, self.state, self.zipcode)
class Classification(ABC):
''' Interface for employee classifications '''
#abstractmethod
def compute_pay(self):
pass
class Hourly(Classification):
''' Manages timecard info. Computes pay '''
def __init__(self, hourly_rate):
self.hourly_rate = hourly_rate
self.timecards = [] # A list of floats representing hours worked
def compute_pay(self):
for i in list_of_timecards:
if i[0] == self.emp_id:
self.timecards.extend(i[1:])
total = list(map(float, self.timecards))
total = sum(total)
self.timecards.clear()
return total * self.hourly_rate
def add_timecard(self, hours):
self.timecards.append(hours)
class Salaried(Classification):
def __init__(self, salary):
self.salary = salary
def compute_pay(self):
return self.salary / 24
class Commissioned(Salaried):
def __init__(self, salary, commission_rate):
self.commission_rate = commission_rate
self.salary = salary
self.receipts = []
def add_receipt(self, amount):
self.receipts.append(amount)
def compute_pay(self):
for i in list_of_receipts:
if i[0] == self.emp_id:
self.receipts.extend(i[1:])
total = list(map(float, self.receipts))
total = sum(total)
self.receipts.clear()
return (self.salary / 24) + ((self.commission_rate / 100) * total)
My understanding of the problem is that I need to pass my 'employee' object to the 'compute_pay' function, which then passes it to the relevant child class (hourly etc...) to run and return the result. I have tried changing
pay = Classification.compute_pay(self)
to
pay = Classification.compute_pay(self.clas)
however that returns error 'AttributeError: 'Employee' object has no attribute 'clas'
which makes no sense. Maybe it is that I am not assigning the employees to the class correctly?
The code for that is (it pulls from a CSV file, and it is pulling the data correctly and generating the class objects, I have checked)
def load_employees():
f = open("employees.csv")
f.readline() # skip header line
for line in f:
fields = line.strip().split(',')
emp = Employee(*fields[:7])
if fields[7] == '3':
clas = Hourly(fields[10]) # Need to define Hourly
emp.classification = clas
elif fields[7] == '2':
clas = Commissioned(fields[8], fields[9])
emp.classification = clas
elif fields[7] == '1':
clas = Salaried(fields[8])
emp.classification = clas
employees.append(emp)
I will figure out your line Classification.compute_pay(self):
Classification => the class Classification
compute_pay => class
method self => this = an Employee instance
pass means do nothing and is used to avoid unneccessary code.
Every class method has self as an argument to allow refering to this instance of the class.
To pass an argument (here your employee) use a parameter. Also implementing a method of the parent class overrides this method.
Every function compute_pay should have a second argument
def compute_pay(self, employee):
# do your stuff
And then you can use this line in issue_payment
pay = self.clas.compute_pay(self)
Two issues here,
Firstly, your Employee instance has two attributes: clas and classification. However, in your constructor, only classification is set.
def __init__(...
...
self.classification = clas
But self.clas is not set to anything. That's why you are getting that error 'Employee' object has no attribute 'clas'. It is only set when one of the make_hourly, make_salaried, or make_commissioned methods are invoked. So when you load the employees CSV, instead of manually creating the instance like you are doing here
clas = Hourly(fields[10])
you should be calling the method make_hourly on your emp instance, like so
emp.make_hourly(fields[10])
It's worth noting that fields[10] is terrible naming. Instead of unpacking all the fields at once, try to unpack them during the for loop:
for a, b, c, d in csv:
...
Secondly, this line of code is wrong in multiple ways
pay = Classification.compute_pay(self)
compute_pay is not a static function or a classmethod. So it shouldn't be called on the Classification class itself, but the Classification instance. This is what you stored in your self.clas attribute. So, compute_pay should be called on self.clas:
def issue_payment(self):
pay = self.clas.compute_pay()
...
In addition to that, when you call a method of a class from inside of another method in the same class, you don't ever need to pass the self argument. It is implied. So even if compute_pay was static or a class method, which it isn't, it would be called like so,
Classification.compute_pay()
Notice there is no self inside the parentheses. Similarly, when you call another method that is not static, self is never passed as an argument:
def my_method(self):
self.another_method()

name error first_name is not defined, after I defined it

I'm trying to make a function that returns a fully formmated full name, here is the code,
def get_formatted_name(first_name, last_name):
"""Return a full name, neatly formatted."""
full_name = (first_name + ' ' + last_name)
return full_name.title()
_musician = get_formatted_name('jimi', 'hendrix')
print(musician)
get_formatted_name(first_name, last_name)
I keep getting an error in the shell, NameError: name 'first_name' is not defined.
You haven't defined first_name anywhere. You have tried to use it without defining it anywhere first.
To get the code above to work you need to get rid of the last line, de-indent the final 2, rename _musician to musician and it will work, as below:
def get_formatted_name(first_name, last_name):
"""Return a full name, neatly formatted."""
full_name = (first_name + ' ' + last_name)
return full_name.title()
musician = get_formatted_name('jimi', 'hendrix')
print(musician)

attributeError: 'list' object has no attribute.....

I am getting attributeError, but I don't understand....
class User():
def __init__(self, first, last, age):
self.first = first
self.last = last
self.age = age
self.login_attempt = 0
class Admin(User):
def __init__(self, first, last, age):
super().__init__(first, last, age)
self.privilages = Privilages()
class Privilages():
def __init__(self, privilages = ''):
self.privilages = []
def show_privilages(self):
print("There are the privilages... : ")
if self.privilages:
for privilage in self.privilages:
print("- " + privilage)
else:
print("The user has no privilages. ")
sarah.privilages = ['can add post', 'can delete post']
sarah.privilages.show_privilages()
I am not sure what I am missing here, I used for loops to go over the list and print it out, however I keep getting error of "'list' object has no attribute 'show_privileges'"
You're assigning a list to sarah.privilages, so it surely does not have a show_privilages method. You should make the __init__ method of Admin take a list of privileges as a parameter, so it can pass on to the __init__ method of Privilages to initialize its privilages attribute:
class Admin(User):
def __init__(self, first, last, age, privilages):
super().__init__(first, last, age)
self.privilages = Privilages(privilages)
class Privilages():
def __init__(self, privilages):
self.privilages = privilages
def show_privilages(self):
print("There are the privilages... : ")
if self.privilages:
for privilage in self.privilages:
print("- " + privilage)
else:
print("The user has no privilages. ")
sarah = Admin('sarah','mary','smith', ['can add post', 'can delete post'])
sarah.privilages.show_privilages()
This outputs:
There are the privilages... :
- can add post
- can delete post
Write a separate Privileges class. The class should have one
attribute, privileges, that stores a list of strings.
Move the show_privileges() method to this class. Make a Privileges instance
as an attribute in the Admin class. Create a new instance of Admin and use your
method to show its privileges
class User():
"""Represent a simple user profile."""
def __init__(self, first_name, last_name, username, email, location):
"""Initialize the user."""
self.first_name = first_name.title()
self.last_name = last_name.title()
self.username = username
self.email = email
self.location = location.title()
class Admin(User):
def __init__(self, first_name, last_name, username, email, location):
super().__init__(first_name, last_name, username, email, location)
#Initialize an empty set of privileges.
self.privileges= Privileges()
class Privileges():
def __init__(self,privileges =[]):
self.privileges = privileges
def Show_privileges(self):
print("\nPrivileges: ")
if self.privileges:
for privilege in self.privileges:
print("- " + str(privilege))
else:
print("- this user has no privileges.")
eric = Admin('suraj', 'boi', 'e_mater', 'e_matthes100#example.com', 'alaska')
eric.describe_user()
eric.privileges.Show_privileges()
print("\nAdding privileges...")
eric_privileges = [
'can reset passwords',
'can moderate discussions',
'can suspend accounts',
]
eric.privileges.privileges = eric_privileges
eric.privileges.Show_privileges()

Categories

Resources