开发者

Having problems showing an attribute from instance class

I know there's a similar post but following it I couldn't solve the problem.

I have a main class 'User', a subclass 'Admin' that inherits the methods from 'User'. Then I have a class 'Priviledges'. Inside 'Admin' there's a 'Priviledges' instance. I want 'Priviledges' to show the attribute 'name' from 'Admin', but it doesn't work. Can you help me?

Sorry I know priviledges, is privileges, english is not my main language.

code:

class User:
    
    def __init__(self, name, last_name, age, city, job):
        self.name = name
        self.last_name = last_name
        self.age = age
        self.city = city
        self.job = job
        self.login_attempts = 0
    
    def describe_user(self):
        print(f"The user's name is {self.name.title()} {self.last_name.title()},\n is {self.age} years old,\n he comes from {self.city.title()},\n开发者_高级运维 his job is {self.job}.")
        print(f"Welcome {self.name.title()}.\n")   

    def increment_login_attempts(self):
        self.login_attempts += 1
        print(f"Login attemps: {self.login_attempts}")
        
    def reset_login_attempts(self):
        self.login_attempts = 0
        print(f"Login attemps: {self.login_attempts}")


class Admin(User):
    def __init__(self, name, last_name, age, city, job):
        super().__init__(name, last_name, age, city, job)
   
        self.prvldgs = Priviledges()        
        

class Priviledges(Admin): 

    def __init__(self, name):
        super().__init__(name)
        
        self.priviledges = ['can add posts', 'can delete posts', 'can ban users']
        
        self.name = name
        
    def show_priviledges(self):
        print(f"{self.name} has these priviledges:\n", self.priviledges)

        
john_admin = Admin('john', 'haward', '32', 'new york', 'writer')

john_admin.prvldgs.show_priviledges()

john_admin.describe_user()

output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [34], in <cell line: 52>()
     43         print(f"{self.name} has these priviledges:\n", self.priviledges)
     50 mark = User('mark', 'hamilton', '23', 'livorno', 'journalist')
---> 52 john_admin = Admin('john', 'haward', '32', 'new york', 'writer')
     55 john_admin.prvldgs.show_priviledges()
     57 john_admin.describe_user()

Input In [34], in Admin.__init__(self, name, last_name, age, city, job)
     27 def __init__(self, name, last_name, age, city, job):
     28     super().__init__(name, last_name, age, city, job)
---> 30     self.prvldgs = Priviledges()

TypeError: __init__() missing 1 required positional argument: 'name'

expected result:

Jhon has these priviledges:
 ['can add posts', 'can delete posts', 'can ban users']
The user's name is John Haward,
 is 32 years old,
 he comes from New York,
 his job is writer.
Welcome John.


class Admin(User):
    def __init__(self, name, last_name, age, city, job):
        super().__init__(name, last_name, age, city, job)
   
        self.prvldgs = Priviledges() 


class Priviledges(Admin): 

    def __init__(self, name):
        super().__init__(name)

To initialize the class Priviledges you need the argument name. You are initializing an instance of it without any parameter: self.prvldgs = Priviledges()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜