简体   繁体   中英

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 ). If raul really is supposed to be a class attribute of Person , you'll have to assign it after the class is defined.

class Person:

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

Person.rahul = Person(100)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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