Python Instantiate Class Within Class Definition

前端 未结 4 1001
小蘑菇
小蘑菇 2021-01-14 14:14

I am attempting to add a variable to a class that holds instances to the class. The following is a shortened version of my code.

class Classy :
    def __in         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-14 14:55

    It seems to me that you want to obtain that:

    class Classy :
        CLASSIES = []
        def __init__(self) :
            self.hi = "HI!"
            Classy.CLASSIES.append(self)
    
    for i in xrange(4):
        Classy()
    
    for x in  Classy.CLASSIES:
        print x
    

    result

    <__main__.Classy instance at 0x011DF3F0>
    <__main__.Classy instance at 0x011DF440>
    <__main__.Classy instance at 0x011DF418>
    <__main__.Classy instance at 0x011DF2B0>
    

    EDIT

    Note that with the code of Lattyware:

    class Classy :
        CLASSIES = []
        idC = id(CLASSIES)
        def __init__(self) :
            self.hi = "HI!"
            #Classy.CLASSIES.append(self)
    
    
    Classy.CLASSIES = [Classy() for _ in xrange(0,4)]
    
    print Classy.idC
    print id(Classy.CLASSIES)
    print 'Classy.idC==id(Classy.CLASSIES) :',Classy.idC==id(Classy.CLASSIES)
    

    result

    18713576
    10755928
    Classy.idC==id(Classy.CLASSIES) : False
    

    While with the for loop of delnan'code, it doesn't appear.

    However it's easy to correct:
    writing
    Classy.CLASSIES[:] = [Classy() for _ in xrange(0,4)]
    or
    Classy.CLASSIES.extend(Classy() for _ in xrange(0,4))
    instead of
    Classy.CLASSIES = [Classy() for _ in xrange(0,4)]
    it depends of what is desired.

    EDIT 2

    Methods may reference global names in the same way as ordinary functions. The global scope associated with a method is the module containing its definition. (A class is never used as a global scope.)

    http://docs.python.org/2/tutorial/classes.html#class-definition-syntax

    A class has a namespace implemented by a dictionary object. Class attribute references are translated to lookups in this dictionary, e.g., C.x is translated to C.__dict__["x"]

    http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes

    class Classy :
        CLASSIES = []
    
    print '"CLASSIES" in globals()',"CLASSIES" in globals()
    print '"CLASSIES" in Classy.__dict__ ==',"CLASSIES" in Classy.__dict__
    

    result

    "CLASSIES" in globals() False
    "CLASSIES" in Classy.__dict__ == True
    

    Delnan, how will you continue to pretend that CLASSIES is global ??
    Did I misunderstood something in your debate with Lattyware ?

提交回复
热议问题