简体   繁体   English

为什么在创建对象时出错?

[英]why do i get error for creating an object?

class Person:

    def __init__(self, ids):
        self.ids = ids

    rahul = Person(100)

error: 错误:

   rahul = Person(100)    
NameError: name 'Person' is not defined

can someone please tell me what the problem is with this simple code? 有人可以告诉我这个简单的代码是什么问题吗?

You are calling Person while the class is still being created (and before the class object is bound to the name Person ). 您在仍在创建类时(在类对象绑定到名称Person )正在调用Person If raul really is supposed to be a class attribute of Person , you'll have to assign it after the class is defined. 如果raul确实应该是Person的类属性,则必须在定义类之后进行分配。

class Person:

    def __init__(self, ids):
        self.ids = ids

Person.rahul = Person(100)

我想象rahul = Person(100)不应该缩进...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM