Here is my opinion. And it's not based on PEP or the Maya SDK api; just my
experience to date.
Sure Python has protected and private concepts as a naming convention only.
But I see properties as sugar for getters/setters(/deleters). I don't
associate them specifically with read-only attributes. To me, they
replicate a C/C++ convention for controlling the read and writes of an
attribute in order to validate the access, or to provide a computed result.
In the first case of controlling access, that would mean you want to be
able to check the value before writing it, or have a hook before reading,
if say it were not yet initialised.
In the second case, you have a value that is dynamically computed. Such as
a "fullname" property that is really just a concatenation of the first and
last name attributes. Or if you want to cache the value after the first
access and then return that cached value on subsequent calls.
So a getter/setter is mostly the same as a property in effect. But
syntactically it routes through attribute access instead of function calls
(if that makes any difference to your API). I use properties for both
dynamic attributes (computed/cached) and read only attributes (no setter),
and/or attributes that are wrapped in caching.

Hope my 2cents is useful.

Justin


On Thu, Aug 6, 2020, 8:40 PM Rudi Hammad <[email protected]> wrote:

> Hello,
> first of all, I've been reading many discussions about when to use what.
> Here is a recap of what I've reasearch:
> .Don't use getters/setters in python, because those are from other idioms.
> getters and setters are meant are use to access private attributes, but
> since in python there is no real encapsulation (it is a naming convention),
> there is no need.
> .Properties are cleaner. E.g: *myNodeA.getx() + myNodeB.getx()* is not as
> clear as *myNodeA.x + myNodeB.x*
> .Use properties only for real only attributes (but doesn't that contradict
> the example above? we can do *myNodeA.x = 5*
> .If someone decide to convert an attribute into a property (e.g, instead
> of *self.myName*, me want a funcion *def myName()*, using @property will
> not break the code.
>
> Will all that info, I still had doubts. So I decided to look at OpenMaya
> 2.0 which is meant to be pythonic. So here are some examples
> *MBoundingBox *has the following as properties --> *center, depth,
> height, max, min, width*. That make sense because those are only readable
> I guess. You can't set them as you want, si it doesn't make sense to do 
> *getCenter(),
> getDepth()*....
> *MDagPath *has no properties at all. It has* .child(), childCount(),
> apiType()*.... but aren't those readable only too?! so why not just *child,
> childCount, apiType.*
> *MObject* has as property *apiTypeStr*, but a method called* apiType() *!!
> what the hell XD? why don't do both as properties?
>
> So yea..looking at OpenMaya 2.0 did not make things more clear.
> Here is how I use properties:
> . I don't like *node.getName()* and *node.setName("foo")*, I prefer*
> node.name <http://node.name>* and *node.name <http://node.name> = "foo"*.
> I find it cleaner.
> . If I have different option of getting something, I need an argument, so
> I don't use property. E.g: *node.getMatrix(world=True),
> node.getMatrix(world=False*).
> . in scientif libraries: I do *vector.magnitude, **vector.magnitude = 10*,
> instead of* vector.getMagnitude(), vector.setMagnitude(10)*
>
> What is your take on that? Cheers,
> R
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/c34f5e0c-2b7a-42a5-856d-84b38ad5f5c3o%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/c34f5e0c-2b7a-42a5-856d-84b38ad5f5c3o%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2Kou3XS9Mu0WQRK5pEy5W_Dqd-jyb45HsPAN6kwyRBYQ%40mail.gmail.com.

Reply via email to