argument checking before getattr

Chris Liechti cliechti at gmx.net
Sat Jan 12 16:28:57 EST 2002


Benjamin Tai <bt98 at doc.ic.ac.uk> wrote in
news:3C408C3E.A8A27FC9 at doc.ic.ac.uk: 
>> > Instead of doing (hiding) argument checking in the interface, now I
>> > am hoping to do some type checking in Python. The following shows
>> > what I am trying to achieve:
>> >
>> > class foo:
>> >       def foo_fun(self,args):
>> >             # do some checking on "args"
>> >             return getattr(self._base,"foo_fun")
>> >
>> > Instead of return the correct argument, it only returns a trace of
>> > the following trace:
>> > <built-in method foo_fun of foo object at 0x80ef820>
>> > It doesn't work out. Why?
>>
>> because getattr gives you the function object in this case.
>> use apply to call the function. or write self._base.foo_fun(args)
> 
> Thanks. It works.
> 
> I guess __getattr__ would apply the function object internally. Would
> it? 

you can say that "()" calls he object on the left of the braces.
so "someobj.foo(args)" uses __getattr__ of someobj to find the member and 
then it applies args on it (calls it with args, there is a builtin function 
apply).

> 
> Heard of the term function object before (I think it was talking about
> lambda). As a beginner, it is one of the area (Python's operation
> semantics, __dict__, etc) which I am trying to get familiar with.
> 
> Please could anyone suggest any tutorial/documentation for them?

there is a list of tutorials and books on the python website. there
are several links in the "Top documentation links" section on the main 
page.

> Cheers
> 
> Ben
> 
> 



-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list