python3 中关于" 'xx' object has no attribute 'xx' " 问题

python3 中关于" ‘xx’ object has no attribute ‘xx’ " 问题

python3 学习中关于" ‘xx’ object has no attribute ‘xx’ " 问题

class Admin(User):
    """为User添加一个privileges的属性"""

    def __index__(self, first_name, last_name, address):
        """
        先初始化父类的属性,再为子类添加一个privileges属性
        """
        super().__init__(first_name,last_name,address)
        self.privileges = ['can do sth']


    def show_privileges(self):
        for privilege in self.privileges:
            print(privilege)

控制台打印如下错误:
python3 中关于
错误原因:
子类构造函数__init__()因为Pycharm联想原因,误选为__index__()。
原因分析:
找不到子类构造函数,所有Python自动去调用父类构造函数,父类构造函数中并没有定义子类的pricileges属性,所以出现错误。

你可能感兴趣的:(Pyhon学习)