Configuration class



  • class Status(object):
         def __init__(self):
             self.__x = 2
    
        def getx(self):
            print("getx is read only")
            return self.__x
        def setx(self, val):
            print("setx is read only")
    
       x = property(getx, setx)
    
    print(Status.x)    #returns <PROPERTY>  what do i do with this
    Status.x=True
    print(Status.x)    
    

    Status.x=False
    print(Status.x)

    Want to create a configution class that i can use to store config info but also have a setter/getter to add logic? Is the above correct?



  • @misterlisty Besides that setx() should store the value, the other way of doing that is with decorators:

    class Status(object):
        def __init__(self):
            self.__x = 2
    
        @property
        def x(self):
            return self.__x
    
        @x.setter
        def x(self, val):
            self.__x=val
    
    a=Status()
    print(a.x)
    a.x=1001
    print(a.x)
    


  • @misterlisty said in Configuration class:

    class Status(object):
    def init(self):
    self.__x = 2

    def getx(self):
        print("getx is read only")
        return self.__x
    def setx(self, val):
        print("setx is read only")
    

    x = property(getx, setx)

    print(Status.x) #returns <PROPERTY> what do i do with this
    Status.x=True
    print(Status.x)

    You are trying to access the CLASS property X, your code should be something like:

    class Status(object):
        def __init__(self):
            self.__x = 2
    
        def getx(self):
            print("getx is read only")
            return self.__x
        def setx(self, val):
            print("setx is read only")
    
        x = property(getx, setx)
    
    
    a = Status()    
    print(a.x)
    a.x=True
    print(a.x)
    

 

Hello World?

Pylife on Kickstarter - November 2018








Back Us On Kickstarter >

Pycom on Twitter