From: Eric J. Van der Velden on 4 Aug 2010 15:58 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.
From: Stefan Schwarzer on 4 Aug 2010 18:36 Hi Eric, On 2010-08-04 21:58, Eric J. Van der Velden wrote: > 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
From: Carl Banks on 4 Aug 2010 19:03 On Aug 4, 12:58 pm, "Eric J. Van der Velden" <ericjvandervel...(a)gmail.com> wrote: > 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
From: John Nagle on 4 Aug 2010 19:28 On 8/4/2010 12:58 PM, Eric J. Van der Velden wrote: > 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
|
Pages: 1 Prev: running a piece of code at specific intervals? Next: BundleBuilder Question |