argument checking before getattr

Benjamin Tai bt98 at doc.ic.ac.uk
Sat Jan 12 14:19:26 EST 2002


Hi Chris,

> > 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?

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?

Cheers

Ben




More information about the Python-list mailing list