1

I am trying to write a code to show the list of privileges for the object User1.

class User: def __init__(self, f_name, l_name, age): self.f_name = f_name self.l_name = l_name self.age = age def describe(self): print(f"The user {self.f_name} {self.l_name} has {self.age} years old person.") def greet(self): print(f"Hi {self.f_name}, you are the Administer right now!") class Privileges: def __init__(self, privileges): self.privileges = ['add post', 'del post', 'ban users'] def show_privileges(self): print(f"These are the Admin list of privileges: {self.privileges}.") class Admin(User): def __init__(self, f_name, l_name, age): super().__init__(f_name, l_name, age) self.privileges = Privileges() user1 = Admin('Porco', 'Rosso', 42) user1.privileges.show_privileges() 

This is the output but I can not find this missing argument:

 Traceback (most recent call last): File "C:\Users\Administrator\Desktop\python_work\teste34.py", line 25, in <module> user1 = Admin('Porco', 'Rosso', 42) File "C:\Users\Administrator\Desktop\python_work\teste34.py", line 23, in __init__ self.privileges = Privileges() TypeError: __init__() missing 1 required positional argument: 'privileges' [Finished in 0.433s] 

I would like to complement with a code I did before where there were no need to pass the argument:

 class User: def __init__(self, f_name, l_name, age): self.f_name = f_name self.l_name = l_name self.age = age def describe(self): print(f"The user {self.f_name} {self.l_name} has {self.age} years old person.") def greet(self): print(f"Hi {self.f_name}, you are now the Admin!") class Admin(User): def __init__(self, f_name, l_name, age): super().__init__(f_name, l_name, age) self.privileges = ['add post', 'del post', 'ban users'] def show_privileges(self): print(f"These are SysOp list of privileges: {self.privileges}.") user1 = Admin('Porco', 'Rosso', 42) user1.describe() user1.greet() user1.show_privileges() 
6
  • 2
    self.privileges = Privileges() you need to pass something in the Privileges(___) or remove privileges from def __init__(self, privileges): Commented Feb 24, 2020 at 11:38
  • 1
    The error message says it explicitly: "__init__() missing 1 required positional argument: 'privileges' - you're calling Privileges class (and thus its __init__ method) without arguments in class Admin. Commented Feb 24, 2020 at 11:39
  • @SayandipDutta thanks but I was thinking that the list (self.privileges = ['add post', 'del post', 'ban users'])would be passed automatic. This is the reason to write instantiate a class as a method. No? Commented Feb 24, 2020 at 11:45
  • 1
    I will write an answer. Commented Feb 24, 2020 at 11:47
  • 1
    @Vinicivs added, please check. Commented Feb 24, 2020 at 11:49

2 Answers 2

3
 self.privileges = Privileges() 

doesn't pass any arguments.

class Privileges: def __init__(self, privileges): 

demands an argument named privileges. It never uses it though, so perhaps you just need to remove it, making it:

class Privileges: def __init__(self): 

If it's supposed to have a default value, you want something like:

class Privileges: def init(self, privileges=('add post', 'del post', 'ban users')): self.privileges = list(privileges)

Note that you shallow copy whatever you were passed to avoid the problems with mutable default arguments and similar issues if a user passes their own list (where you don't want your modifications to affect them, or vice-versa).

Sign up to request clarification or add additional context in comments.

Comments

0

According your comments, you were meaning to pass the argument in the __init__ of class Priviliges implicitly. In order to do that, you needed a default argument:

class User: def __init__(self, f_name, l_name, age): self.f_name = f_name self.l_name = l_name self.age = age def describe(self): print(f"The user {self.f_name} {self.l_name} has {self.age} years old person.") def greet(self): print(f"Hi {self.f_name}, you are the Administer right now!") class Privileges: def __init__(self, privileges = ['add post', 'del post', 'ban users']): self.privileges = privileges def show_privileges(self): print(f"These are the Admin list of privileges: {self.privileges}.") class Admin(User): def __init__(self, f_name, l_name, age): super().__init__(f_name, l_name, age) self.privileges = Privileges() user1 = Admin('Porco', 'Rosso', 42) user1.privileges.show_privileges() 

Output:

These are the Admin list of privileges: ['add post', 'del post', 'ban users']. 

NOTE

However, using mutable types as default argument is strongly discouraged.

2 Comments

Your default is risking serious problems.
@ShadowRanger I know, I should add not recommended in the answer body.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.