lambda functions as class member

456 views
Skip to first unread message

Bjorn Pettersen

unread,
Oct 7, 1997, 3:00:00 AM10/7/97
to

>

I'm new to Python (so correct me if I'm wrong...), but I think the scope of lambda is the global
scope plus its own scope, i.e. 'self' is not in its scope. "lambda index, self=self: ..." should
work.

-- bjorn

> class Y(X):
> def __init__(self):
> self.fix_names = ("fix_name", "A")
> for name in self.fix_names:
> exec 'self.A_' + name + ' = \
> lambda index: self.A("' + name + '", index)'
>
> but when I try to use instance.A_nameX I get
>
> File "<string>", line 1, in <lambda>
> NameError: self
>

------------------------------
namespace bjorn {
const bool speaks_for_employer = false;
}
------------------------------


Berthold Hoellmann

unread,
Oct 7, 1997, 3:00:00 AM10/7/97
to

Hello,

I have a group of classes derived from the same class. The base
class has a member function "A(self, name, index)" which
I want to use in the derived classes

class X:
def A(self, name, index):
print name, index

Each derived class has a set of these A-type functions. To ease
the maintanance of these functions and because I need the set
of fix_name's I tried :

class Y(X):
def __init__(self):
self.fix_names = ("fix_name", "A")
for name in self.fix_names:
exec 'self.A_' + name + ' = \
lambda index: self.A("' + name + '", index)'

but when I try to use instance.A_nameX I get

File "<string>", line 1, in <lambda>
NameError: self

class Y(X):
def A_fix_name(self, index):
return self.A("fix_name", index)

is OK.
Has anyone a good solution for me?

Cheers
Berthold
--
email: ho...@hamburg.GermanLloyd.de
)
(
C[_] These opinions might be mine, but never those of my employer.

Samuel L. Bayer

unread,
Oct 8, 1997, 3:00:00 AM10/8/97
to

In article <343A2D...@hamburg.GermanLloyd.de>, Berthold Hoellmann <ho...@hamburg.GermanLloyd.de> writes:
[...]

|> class Y(X):
|> def __init__(self):
|> self.fix_names = ("fix_name", "A")
|> for name in self.fix_names:
|> exec 'self.A_' + name + ' = \
|> lambda index: self.A("' + name + '", index)'
|>
|> but when I try to use instance.A_nameX I get
|>
|> File "<string>", line 1, in <lambda>
|> NameError: self

Here's essentially what you defined:

class Y(X):
def __init__(self):
self.A_fix_name = lambda index: self.A("fix_name", index)

But lambda doesn't inherit the environment that it's evaluated in. What
you want to do is use a famous Python trick which exploits optional
arguments to pass in external environments to lambda:

class Y(X):
def __init__(self):
self.A_fix_name = lambda index, s = self: s.A("fix_name", index)

So what you want is:

class Y(X):
def __init__(self):
self.fix_names = ("fix_name", "A")
for name in self.fix_names:
exec 'self.A_' + name + ' = \

lambda index, s = self: s.A("' + name + '", index)'

And this works:

>>> class X:
... def A(self, name, index):
... print name, index
...
>>> class Y(X):
... def __init__(self):
... self.fix_names = ("fix_name", "A")
... for name in self.fix_names:
... exec 'self.A_' + name + ' = lambda index, s = self: s.A("' + name + '", index)'
...
>>> instance = Y()
>>> instance.A_fix_name(4)
fix_name 4

Cheers,
Sam Bayer

Reply all
Reply to author
Forward
0 new messages