Best way to set/get an object property

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hussein B

    Best way to set/get an object property

    Hey,
    I noted that Python encourage the usage of:
    --
    obj.prop = data
    x = obj.prop
    --
    to set/get an object's property value.
    What if I want to run some logic upon setting/getting a property?
    What is Python preferred method to do so (using the new feature
    'property')?
    I don't think __getattr__ and __setattr__ are practical (I have to
    code the property name into them).
    Thanks.
  • Peter Otten

    #2
    Re: Best way to set/get an object property

    Hussein B wrote:
    I noted that Python encourage the usage of:
    --
    obj.prop = data
    x = obj.prop
    --
    to set/get an object's property value.
    What if I want to run some logic upon setting/getting a property?
    What is Python preferred method to do so (using the new feature
    'property')?
    I don't think __getattr__ and __setattr__ are practical (I have to
    code the property name into them).
    Hussein, I don't think you'll learn much from asking these abstract
    questions. At some point you have to get your hands dirty and write actual
    code to get a feel for the language.

    For example, it will then become obvious for you that property works best
    for individual attributes while __getattr__ and friends are more convenient
    if you want to treat multiple attributes the same way, attributes whose
    names may not even be known until runtime (think delegation).

    Peter

    Comment

    • Hussein B

      #3
      Re: Best way to set/get an object property

      On Aug 24, 5:28 am, Peter Otten <__pete...@web. dewrote:
      Hussein B wrote:
      I noted that Python encourage the usage of:
      --
      obj.prop = data
      x = obj.prop
      --
      to set/get an object's property value.
      What if I want to run some logic upon setting/getting a property?
      What is Python preferred method to do so (using the new feature
      'property')?
      I don't think __getattr__ and __setattr__ are practical (I have to
      code the property name into them).
      >
      Hussein, I don't think you'll learn much from asking these abstract
      questions. At some point you have to get your hands dirty and write actual
      code to get a feel for the language.
      >
      For example, it will then become obvious for you that property works best
      for individual attributes while __getattr__ and friends are more convenient
      if you want to treat multiple attributes the same way, attributes whose
      names may not even be known until runtime (think delegation).
      >
      Peter
      Thanks Peter,
      You are right, I have to try to touch the Python but the problem is I
      don't have much time to do so.
      I have a Java developer for more than 4 years and I find it is not so
      easy to digest Python concepts, this is why I'm asking a lot of
      obvious and clear easy to you (long time Pythonists).
      Thank you for your time.

      Comment

      • castironpi

        #4
        Re: Best way to set/get an object property

        On Aug 24, 5:07 am, Hussein B <hubaghd...@gma il.comwrote:
        Hey,
        I noted that Python encourage the usage of:
        --
        obj.prop = data
        x = obj.prop
        --
        to set/get an object's property value.
        What if I want to run some logic upon setting/getting a property?
        What is Python preferred method to do so (using the new feature
        'property')?
        I don't think __getattr__ and __setattr__ are practical (I have to
        code the property name into them).
        Thanks.
        The answer Hussein is you have both options in Python. If neither one
        is clearly better-suited to your new application, pick one and go.

        Comment

        • Steven D'Aprano

          #5
          Re: Best way to set/get an object property

          On Sun, 24 Aug 2008 12:28:53 +0200, Peter Otten wrote:
          Hussein B wrote:
          >
          >I noted that Python encourage the usage of: --
          >obj.prop = data
          >x = obj.prop
          >--
          >to set/get an object's property value. What if I want to run some logic
          >upon setting/getting a property? What is Python preferred method to do
          >so (using the new feature 'property')?
          >I don't think __getattr__ and __setattr__ are practical (I have to code
          >the property name into them).
          >
          Hussein, I don't think you'll learn much from asking these abstract
          questions. At some point you have to get your hands dirty and write
          actual code to get a feel for the language.
          >
          For example, it will then become obvious for you that property works
          best for individual attributes while __getattr__ and friends are more
          convenient if you want to treat multiple attributes the same way,
          attributes whose names may not even be known until runtime (think
          delegation).

          I think you are misunderstandin g Hussein's question. I believe that he is
          using "property" to refer to what we would call an attribute. Naturally I
          could be wrong, but this is how I interpret his question.

          I think the actual answer to his question is that properties are the
          preferred way to "run some logic upon setting/getting" an attribute, that
          is, to implement getters and setters.

          Hussein, the Java habit of writing setters and getters for everything
          isn't considered good practice in Python, but if you need them, that's
          exactly what the property() function is for.



          --
          Steven

          Comment

          • Hussein B

            #6
            Re: Best way to set/get an object property

            On Aug 24, 7:12 pm, Steven D'Aprano <st...@REMOVE-THIS-
            cybersource.com .auwrote:
            On Sun, 24 Aug 2008 12:28:53 +0200, Peter Otten wrote:
            Hussein B wrote:
            >
            I noted that Python encourage the usage of: --
            obj.prop = data
            x = obj.prop
            --
            to set/get an object's property value. What if I want to run some logic
            upon setting/getting a property? What is Python preferred method to do
            so (using the new feature 'property')?
            I don't think __getattr__ and __setattr__ are practical (I have to code
            the property name into them).
            >
            Hussein, I don't think you'll learn much from asking these abstract
            questions. At some point you have to get your hands dirty and write
            actual code to get a feel for the language.
            >
            For example, it will then become obvious for you that property works
            best for individual attributes while __getattr__ and friends are more
            convenient if you want to treat multiple attributes the same way,
            attributes whose names may not even be known until runtime (think
            delegation).
            >
            I think you are misunderstandin g Hussein's question. I believe that he is
            using "property" to refer to what we would call an attribute. Naturally I
            could be wrong, but this is how I interpret his question.
            >
            I think the actual answer to his question is that properties are the
            preferred way to "run some logic upon setting/getting" an attribute, that
            is, to implement getters and setters.
            >
            Hussein, the Java habit of writing setters and getters for everything
            isn't considered good practice in Python, but if you need them, that's
            exactly what the property() function is for.
            >
            --
            Steven
            Thank you Steven :)
            --
            public class JClass {
            private int answer; // property
            }
            --
            class PyClass(object) :
            doc __init__(self):
            self.answer = None
            --
            AFAIUY (understand you), what it is called a property in Java, it is
            called an attribute in Python?
            Why Python encourages direct access to object's attributes? aren't
            setters/getters considered vital in OOP (encapsulation) ?
            Thank you all for your time and help.

            Comment

            • alex23

              #7
              Re: Best way to set/get an object property

              On Aug 25, 4:56 pm, Hussein B <hubaghd...@gma il.comwrote:
              AFAIUY (understand you), what it is called a property in Java, it is
              called an attribute in Python?
              Why Python encourages direct access to object's attributes?
              The simplest answer is "Because Python is not Java" :)

              Speaking of which, have you read the blog post of the same name? It
              might be useful given your Java background: http://dirtsimple.org/2004/12/python-is-not-java.html
              > aren't
              setters/getters considered vital in OOP (encapsulation) ?
              Not at all. They're definitely part of the mechanism that Java
              provides for encapsulation, sure. However, because Python provides a
              consistent interface for accessing attributes and properties, you
              don't need to define a property unless your code requires it. If all
              your getters & setters are doing is reading & writing to an attribute,
              then why not just r&w directly to the attribute? If you later need to
              add more complexity to that process, you can easily create a property
              without having to change how any other piece of code refers to that
              property, given it shares the same interface with attributes.

              Comment

              • Ken Seehart

                #8
                Re: Best way to set/get an object property

                This is probably what you want:



                Available in Python 2.2 or later.

                Enjoy,
                Ken Seehart

                Hussein B wrote:
                Hey,
                I noted that Python encourage the usage of:
                --
                obj.prop = data
                x = obj.prop
                --
                to set/get an object's property value.
                What if I want to run some logic upon setting/getting a property?
                What is Python preferred method to do so (using the new feature
                'property')?
                I don't think __getattr__ and __setattr__ are practical (I have to
                code the property name into them).
                Thanks.
                --

                >
                >

                Comment

                • Bruno Desthuilliers

                  #9
                  Re: Best way to set/get an object property

                  Steven D'Aprano a écrit :
                  (snip)
                  But it's quite rare to see double-underscore "really private" attributes
                  in Python code. It is considered to go against the spirit of the language.
                  Not necessarily "against the spirit" - it's mostly than __name_mangling
                  is only really useful when you want to protect a really vital
                  implementation attribute from being *accidentaly* overridden, and mostly
                  annoying anywhere else.
                  I'm told that in Java it is quite difficult to change a class from using
                  public attributes to getters/setters,
                  That's an understatement. Java has *no* support for computed attributes,
                  so you just can *not* turn a public attribute into a computed one.
                  and therefore many Java developers
                  prefer to use getters/setters right from the beginning.
                  Truth is that they have no other choice if they want to be able to
                  decouple implementation from interface.

                  Comment

                  Working...