to pass self or not to pass self
Rami Chowdhury
rami.chowdhury at gmail.com
Mon Mar 15 14:01:00 EDT 2010
On Monday 15 March 2010 10:42:41 TomF wrote:
> On 2010-03-15 09:39:50 -0700, lallous <elias.bachaalany at gmail.com> said:
> >
> > Why in test1() when it uses the class variable func_tbl we still need
> > to pass self, but in test2() we don't ?
> >
> > What is the difference between the reference in 'F' and 'func_tbl' ?
>
> I recommend putting print statements into your code like this:
>
> def test1(self, n, arg):
> print "In test1, I'm calling a %s" % self.func_tbl[n]
> return self.func_tbl[n](self, arg)
>
> def test2(self):
> f = self.f1
> print "Now in test2, I'm calling a %s" % f
> f(6)
>
>
> Bottom line: You're calling different things. Your func_tbl is a dict
> of functions, not methods.
>
> -Tom
To build on that a bit, note that in test2() you are doing:
> > f = self.f1
> > f(6)
> >
> > f = self.F
> > # why passing self is not needed?
> > f(87)
As I understand it, since you obtained the reference to 'f1' from 'self', you
got it as a bound rather than an unbound method. So 'self' is automatically
passed in as the first argument.
----
Rami Chowdhury
"Given enough eyeballs, all bugs are shallow." -- Linus' Law
408-597-7068 (US) / 07875-841-046 (UK) / 01819-245544 (BD)
More information about the Python-list
mailing list