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()
Vinicivs
  • 191
  • 1
  • 7
  • 2
    `self.privileges = Privileges()` you need to pass something in the `Privileges(___)` or remove `privileges` from `def __init__(self, privileges):` – Sayandip Dutta Feb 24 '20 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`. – Błotosmętek Feb 24 '20 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? – Vinicivs Feb 24 '20 at 11:45
  • 1
    I will write an answer. – Sayandip Dutta Feb 24 '20 at 11:47
  • 1
    @Vinicivs added, please check. – Sayandip Dutta Feb 24 '20 at 11:49
  • @SayandipDutta thank you very much. I am still trying to understand but I became more confused since the code a wrote before did not need to pass anything. Could you check this other code in the end of post? It runs ok. – Vinicivs Feb 24 '20 at 12:12

2 Answers2

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).

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
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.

Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52