__init__ as a lambda

  • Thread starter Eric J. Van der Velden
  • Start date
E

Eric J. Van der Velden

Hello,

Suppose

class C:
def __init__(self,name):self.name=name

I was wondering if I could make the __init__ a lambda function, but

class C:
__init__=lambda self,self.name:None

and then later,

C('Hello')

does not work; the first argument, self, is assigned all rigth, but
you cannot write the second argument with a dot, self.name .
Or can I somehow?

Thanks,

Eric J.
 
S

Stefan Schwarzer

Hi Eric,

class C:
def __init__(self,name):self.name=name

I was wondering if I could make the __init__ a lambda function, but

class C:
__init__=lambda self,self.name:None

and then later,

C('Hello')

does not work; the first argument, self, is assigned all rigth, but
you cannot write the second argument with a dot, self.name .

The "problem" is that in a lambda function the part after
the colon has to be an expression. However, you have used
an assignment there which isn't an expression in Python but
a statement.

For example, you can use

f = lambda x: sys.stdout.write(str(x))

(sys.stdout.write(str(x)) is an expression)

but not

f = lambda x: print x

(print x is a statement in Python versions < 3)

Stefan
 
C

Carl Banks

Hello,

Suppose

class C:
 def __init__(self,name):self.name=name

I was wondering if I could make the __init__ a lambda function, but

class C:
 __init__=lambda self,self.name:None

and then later,

C('Hello')

does not work; the first argument, self, is assigned all rigth, but
you cannot write the second argument with a dot,  self.name .
Or can I somehow?


__init__=lambda self,name:setattr(self,'name',name)

However if you actually do this, you need to be smacked upside the
head.


Carl Banks
 
J

John Nagle

Hello,

Suppose

class C:
def __init__(self,name):self.name=name

I was wondering if I could make the __init__ a lambda

Python is not a functional language. Attempts to make
it one make it worse.

There's this mindset that loops are somehow "bad".
This leads to list comprehensions, multiline lambdas, more
elaborate generators, weird conditional expression
syntax, and related cruft. Most of these features are of
marginal, if not negative, value.

Unfortunately, some of them have gone into Python.

John Nagle
 
S

Steven D'Aprano

Hello,

Suppose

class C:
def __init__(self,name):self.name=name

I was wondering if I could make the __init__ a lambda function,

Of course you can. Lambdas aren't special types of functions, they are
*syntax* for creating a function consisting of a single expression, and
your __init__ function is a single expression.

These two are almost identical:

def spam(a, b):
return a+b


spam = lambda a, b: a+b

The only(?) differences are spam.func_name or spam.__name__.

but

class C:
__init__=lambda self,self.name:None

and then later,

C('Hello')

does not work; the first argument, self, is assigned all rigth, but you
cannot write the second argument with a dot, self.name .

That gives a syntax error no matter whether you use it in a lambda form
or an ordinary function:
File "<stdin>", line 1
def f(self,self.name):
^
SyntaxError: invalid syntax


So the problem has nothing to do with lambda. What you want is:

lambda self: self.name = None

but of course that doesn't work either, because self.name = None is not
an expression, it's a statement. So:


class C:
__init__ = lambda self: setattr(self, 'name', None)


But don't do this. Seriously. Just because it is syntactically valid and
does what you want, doesn't mean you should do it. Unless you have really
good reason, and saving a single line of source code is a *bad* reason,
just stick to the standard idiom that everyone can read without going
"WTF is this guy doing this for???".

class C:
def __init__(self):
self.name = None
 

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,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top