AttributeError: 'Person' 对象没有属性 'name' 错误请帮帮我

六月地带
class Person:
    def __init__(self, name, age, profession):
        # data members (instance variables)
        self.name == name
        self.age == age
        self.prof == profession

#Behavior(instance methods)
def _show(self):
    print('Name:', self.name, 'Age:', self.age, 'Profession:', self.profession)

# Behavior (instance methods)
def _work(self):
    print(self.name, 'working as a', self.prof)

# create an object of a class
john = Person('John', 19, 'Robotics Engineer')

# call methods
john.show()
john.work()

AttributeError:“人”对象没有属性“名称”

他出发了

您在标题中提到的错误正在发生,因为您部分地通过具有 2 个等号而不是分配它们来比较值( __init__self.name == nameself.name = name

并且还在类中定义函数 show & work以便访问它。

正确代码:

class Person:
    
    def __init__(self, name, age, profession):
        # data members (instance variables)
        self.name = name
        self.age = age
        self.prof = profession

    #Behavior(instance methods)
    def show(self):
        print('Name:', self.name, 'Age:', self.age, 'Profession:', self.prof)
    
    # Behavior (instance methods)
    def work(self):
        print(self.name, 'working as a', self.prof)

# create an object of a class
john = Person('John', 19, 'Robotics Engineer')

# call methods
john.show()
john.work()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

AttributeError: 'Person' 对象没有属性 'heal'

范围函数没有迭代整个列表,请帮帮我

AttributeError类型的对象Person没有属性ID

AttributeError:“ str”对象没有属性“ name”

AttributeError:'Doctype'对象没有属性'name'

无法将项目添加到集合属性。请帮帮我,拜托

'function'对象没有属性'objects'Django,帮帮我

收到错误AttributeError:'FlashDisplayPage'对象没有属性'label_name'

Python 3导入错误AttributeError:从C ++调用脚本时,'_ModuleLock_'对象没有属性'name'

AttributeError:“模块”对象没有属性“错误”

卡在这个JAVA多维数组问题上有一段时间了,请帮帮我

AttributeError:'SparseCategoricalCrossentropy'对象没有属性'__name__'

轻松应用:AttributeError:'DataFrame'对象没有属性'name'

AttributeError:“ str”对象没有属性“ __name__”

AttributeError:“ LGBMRegressor”对象没有属性“ feature_name_”

Keras:AttributeError:'Adam'对象没有属性'_name'

AttributeError: 'str' 对象没有属性 'name' PySpark

AttributeError: 'DataFrame' 对象在使用 SMOTE 时没有属性 'name'

AttributeError: 'Animal' 对象没有属性 'get_name'

AttributeError: 'Seekerskillset' 对象没有属性 'skill_name'

Web 抓取 Selenium 错误:“带有 AttributeError:'list' 对象没有属性 'find_element_by_class_name'”

如何修复 'AttributeError:'Person' 对象在 Django is_valid() 方法中没有属性 '_default_manager'

我的 kivy 应用程序运行此错误: KeyError: 'container' 有人可以帮帮我吗?

我在使用 pygame 时遇到问题,我试图从 VScode 运行部分输出,但没有显示它。请帮帮我?

AttributeError : 'RMSProp' 没有属性 'name'

奇怪的“AttributeError:‘NoneType’对象没有属性‘组’”错误

Python错误:AttributeError:'str'对象没有属性'k'

AttributeError: 'str' 对象没有属性 'strftime' 面临错误

opencv lib 错误:AttributeError: 'NoneType' 对象没有属性 'shape'