RE: new minor getAttr feature
"Mark Hahn" <[email protected]>
| Newsgroups | gmane.comp.lang.prothon.user |
|---|---|
| Message-ID | <000e01c46904$3f5a1370$0b01a8c0@mark> |
Lenard Lindstrom wrote:
> In Python the getattr function has matching setattr and
> delattr functions for setting and deleting attributes respectively.
Yes, but I thought attrs_ would suffice for setAttr() and delAttr()
unless you want to handle the prototype chain cases. Then you should be
explicit and use getAttr() first.
> The non-super version of getAttr is simply obj.attrs_[name] .
>
> getAttr should through a NameError if the attribute is not
> found since None is a valid attribute value.
Yes, I will change that. That was stupid of me.
> And I guess getAttr, like attrs_, will be a truly special
> method, uneffected by wild-card property methods. I had not
> noticed it before but the following does not cause infinite recursion:
>
> O>> object Wild:
> ... def get_(attr):
> ... print("Getting", attr)
> ... return self.attrs_[attr]
> ... d = 8
> ... e = 9
> ...
> 9
> O>> Wild.d
> Getting $d
> 8
> O>> Wild.e
> Getting $e
> 9
>
> I guess there is no way around these magic attributes since
> there are no classes, which bypass the property mechanism.
> Alternatively, one could have functions.
>
> getAttr(obj, name)
> getAttrs(obj) # Return object attribute list; replaces attrs_
> setAttr(obj, name, value)
> delAttr(obj, name)
>
> Nothing magical about these. But I would have these do normal
> attribute lookup, which means honouring properties.
>
> To bypass properties I would have special getAttrIgnoreProp,
> setAttrIgnoreProp and delAttrIgnoreProp functions. They have
> verbose names but they are for special use only.
Do you really need all these functions? How about:
value = getAttr(name)
(value, cont) = getAttr(name, cont=True)
tuple[1].name = newValue # replaces setAttr()
del tuple[1].name # replaces delAttr()
Then we would have two functions total:
getAttr(name, cont=False, super=None)
getAttrNoProp(name, cont=False, super=None)