getattr

K

kyaBey

This question is regarding the __getattr__ function defined for every
object.
Consider the following example

Assume that foo is an instance of class Foo, and the following are
references to foo's field "bar" which is an instance of class Bar

a) foo.bar
b) foo.bar.spam - spam is a member of "bar"

I want the above references to be handled by __getattr__. Hence I do
not have an entry for the 'bar' in foo.__dict__

Is there any way by which the __getattr__(self,attr) method can
determine that in
case a) attr == 'bar' is the final component in the reference unlike in
case b) where attr=='bar' is NOT the ultimate(final) component of
reference and is an intermediate component in the reference.

tia
 
F

Fredrik Lundh

Is there any way by which the __getattr__(self,attr) method can
determine that in
case a) attr == 'bar' is the final component in the reference unlike in
case b) where attr=='bar' is NOT the ultimate(final) component of
reference and is an intermediate component in the reference.

no.

if you want to control further accesses, your __getattr__ has to return a
proxy object, and use a suitable syntax to get the final value.

</F>
 
F

Fredrik Lundh

if you want to control further accesses, your __getattr__ has to return a
proxy object, and use a suitable syntax to get the final value.

message.insert(index, "your users have to ")

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top