繁体   English   中英

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

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

class Person:

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

    rahul = Person(100)

错误:

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

有人可以告诉我这个简单的代码是什么问题吗?

您在仍在创建类时(在类对象绑定到名称Person )正在调用Person 如果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