Re: new minor getAttr feature

Lenard Lindstrom <[email protected]>
Newsgroups gmane.comp.lang.prothon.user
Message-ID <Mahogany-0.66.0-4294665815-20040713-101853.00@pop3.norton.antivirus>
On Mon, 12 Jul 2004 21:37:41 -0700 Mark Hahn <[email protected]> wrote:

> http://prothon.org/wiki?pagename=Object.getAttr%28%29
> 
> This will be in the next interim release.
>
In Python the getattr function has matching setattr and delattr
functions for setting and deleting attributes respectively.

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.

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.

Lenard Lindstrom
<[email protected]>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.