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 said in Configuration class:
class Status(object):
def init(self):
self.__x = 2def 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) -
@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)
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login