Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Configuration class

    Scheduled Pinned Locked Moved MicroPython
    python
    3 Posts 3 Posters 884 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      misterlisty
      last edited by

      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?

      robert-hhR 1 Reply Last reply Reply Quote 0
      • sebS Offline
        seb
        last edited by

        @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)
        
        1 Reply Last reply Reply Quote 0
        • robert-hhR Offline
          robert-hh @misterlisty
          last edited by robert-hh

          @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)
          
          1 Reply Last reply Reply Quote 0

          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
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom