Re: Default method arguments

2005-11-18 Thread Peter Otten
Martin Miller wrote: > Well, perhaps the same in the sense of name binding, but there's a > subtle difference in replacing the 's = [n]'  with 'foo.s = n'.  Namely > that in the former case (with the essay's original code) a separate > container is created when foo() is first called and is what is

Re: Default method arguments

2005-11-18 Thread Gregory Petrosyan
Thanks Martin, you are right. -- http://mail.python.org/mailman/listinfo/python-list

Re: Default method arguments

2005-11-17 Thread Martin Miller
Mike Meyer wrote, in part:: > "Gregory Petrosyan" <[EMAIL PROTECTED]> writes: > ... > > 2) Is 'foo.s = n' a correct solution? It seems to be a little more > > elegant. (I tested it, and it worked well) > > It's basically the same solution. You're replacing binding a variable > with mutating an obj

Re: Default method arguments

2005-11-16 Thread Duncan Booth
Steven D'Aprano wrote: > My philosophy is, any time you have an object that has a magic meaning > (e.g. as a sentinel), don't tempt your users to try to use it as if it > were an ordinary object. In that case the simplest thing is to give _marker a more appropriate name such as '_use_late_bound_

Re: Default method arguments

2005-11-16 Thread Fredrik Lundh
Duncan Booth wrote: > What you really want is for the marker to exist only in its own little > universe, but the code for that is even messier: > > class A(object): >def __init__(self, n): >self.data =n >def make_f(): >marker = object() >def f(self, x = _marker): N

Re: Default method arguments

2005-11-16 Thread Steven D'Aprano
On Wed, 16 Nov 2005 09:48:47 +, Duncan Booth wrote: > Steven D'Aprano wrote: >> I would like to see _marker put inside the class' scope. That prevents >> somebody from the outside scope easily passing _marker as an argument >> to instance.f. It also neatly encapsulates everything A needs withi

Re: Default method arguments

2005-11-16 Thread Steven D'Aprano
On Wed, 16 Nov 2005 02:59:15 +, Bengt Richter wrote: > On Tue, 15 Nov 2005 23:51:18 +0100, "Fredrik Lundh" <[EMAIL PROTECTED]> wrote: >>> I would like to see _marker put inside the class' scope. That prevents >>> somebody from the outside scope easily passing _marker as an argument >>> to ins

Re: Default method arguments

2005-11-16 Thread Duncan Booth
Steven D'Aprano wrote: > I would like to see _marker put inside the class' scope. That prevents > somebody from the outside scope easily passing _marker as an argument > to instance.f. It also neatly encapsulates everything A needs within > A. Surely that makes it easier for someone outside the s

Re: Default method arguments

2005-11-16 Thread [EMAIL PROTECTED]
What you want is essentially : if parm_x is not supplied, use self.val_x So why not just express it clearly at the very beginning of the function : def f(self, parm_x=NotSupplied, parm_y=NotSupplied ,,,) if parm_x is NotSupplied: parm_x = self.val_x if parm_y is NotSupplied: parm_y = self.va

Re: Default method arguments

2005-11-15 Thread Bengt Richter
On 15 Nov 2005 11:02:38 -0800, "Martin Miller" <[EMAIL PROTECTED]> wrote: >Alex Martelli wrote, in part: >> If it's crucial to you to have some default argument value evaluated at >> time X, then, by Python's simple rules, you know that you must arrange >> for the 'def' statement itself to execute

Re: Default method arguments

2005-11-15 Thread Bengt Richter
On Tue, 15 Nov 2005 23:51:18 +0100, "Fredrik Lundh" <[EMAIL PROTECTED]> wrote: >Steven D'Aprano wrote: > >>> Another solution to this is the use of a 'marker' object and identity test: >>> >>> _marker = [] >>> class A(object): >>> def __init__(self, n): >>> self.data =n >>> def f(s

Re: Default method arguments

2005-11-15 Thread Fredrik Lundh
Steven D'Aprano wrote: >> Another solution to this is the use of a 'marker' object and identity test: >> >> _marker = [] >> class A(object): >> def __init__(self, n): >> self.data =n >> def f(self, x = _marker): >> if x is _marker: >> x = self.data >> pr

Re: Default method arguments

2005-11-15 Thread Steven D'Aprano
On Tue, 15 Nov 2005 18:44:23 +0100, bruno at modulix wrote: > Another solution to this is the use of a 'marker' object and identity test: > > _marker = [] > class A(object): > def __init__(self, n): > self.data =n > def f(self, x = _marker): > if x is _marker: >

Re: Default method arguments

2005-11-15 Thread Gregory Petrosyan
Thanks a lot, I understood the rule. Let's don't discuss this (containers etc.) anymore, or it'll be offtopic. -- http://mail.python.org/mailman/listinfo/python-list

Re: Default method arguments

2005-11-15 Thread Mike Meyer
"Gregory Petrosyan" <[EMAIL PROTECTED]> writes: > I'm not very familiar with Python, so please explain me why should > containers be used? > For example in one of Paul Graham's essays there's an example of > 'generator of accumulators' in Python: > > def foo(n): > s = [n] > def bar(i): >

Re: Default method arguments

2005-11-15 Thread Gregory Petrosyan
I'm not very familiar with Python, so please explain me why should containers be used? For example in one of Paul Graham's essays there's an example of 'generator of accumulators' in Python: def foo(n): s = [n] def bar(i): s[0] += i return s[0] return bar 1) So, why

Re: Default method arguments

2005-11-15 Thread Mike Meyer
Benji York <[EMAIL PROTECTED]> writes: > I'll add my 2 cents to the mix: > > default = object() > > class A(object): > def __init__(self, n): > self.data = n > > def f(self, x=default): > if x is default: > x = self.data > print x There were a lot

Re: Default method arguments

2005-11-15 Thread Mike Meyer
[EMAIL PROTECTED] writes: > Hello everybody! > I have little problem: > > class A: > def __init__(self, n): > self.data = n > def f(self, x = ) > print x > > All I want is to make self.data the default argument for self.f(). (I > want to use 'A' class as following : St

Re: Default method arguments

2005-11-15 Thread Benji York
bruno at modulix wrote: > Another solution to this is the use of a 'marker' object and identity test: > > _marker = [] > class A(object): > def __init__(self, n): > self.data =n > def f(self, x = _marker): > if x is _marker: > x = self.data > p

Re: Default method arguments

2005-11-15 Thread Gregory Petrosyan
Great thanks, Alex! -- http://mail.python.org/mailman/listinfo/python-list

Re: Default method arguments

2005-11-15 Thread Martin Miller
Alex Martelli wrote, in part: > If it's crucial to you to have some default argument value evaluated at > time X, then, by Python's simple rules, you know that you must arrange > for the 'def' statement itself to execute at time X. In this case, for > example, if being able to have self.data as th

Re: Default method arguments

2005-11-15 Thread Duncan Booth
Nicola Larosa wrote: >> Using None might be problematic if None could be a valid argument. > > That's like saying that NULL could be a significant value in SQL. In > Python, "None" *is* the empty, not significant value, and should > always be used as such. Specifically, never exchange "None" for

Re: Default method arguments

2005-11-15 Thread bruno at modulix
Dennis Lee Bieber wrote: > > (snip) > but that may not be desirable if None is a valid value => myA.f(None), > so... > > class A(object): > def __init__(self, n): > self.data =n > def f(self, *arg): > if len(arg) == 0: > x = self.data > else: >

Re: Default method arguments

2005-11-15 Thread Alex Martelli
Gregory Petrosyan <[EMAIL PROTECTED]> wrote: ... > def f(self, x = self.data) (*) ... > So I think (*) is the best variant, but it doesn't work :( It DOES work -- it just does not work the way you _expect_ it to work, but rather, it works the way it's _defined_ to w

Re: Default method arguments

2005-11-15 Thread Gregory Petrosyan
Thanks a lot, but that's not what I do really want. 1) f() may have many arguments, not one 2) I don't whant only to _print_ x. I want to do many work with it, so if I could simply write def f(self, x = self.data) (*) it would be much better. By the way, using class A(

Re: Default method arguments

2005-11-15 Thread bruno at modulix
[EMAIL PROTECTED] wrote: > Hello everybody! > I have little problem: > > class A: > def __init__(self, n): > self.data = n > def f(self, x = ) > print x > > All I want is to make self.data the default argument for self.f(). (I > want to use 'A' class as following : >

Re: Default method arguments

2005-11-15 Thread Nicola Larosa
> Using None might be problematic if None could be a valid argument. That's like saying that NULL could be a significant value in SQL. In Python, "None" *is* the empty, not significant value, and should always be used as such. Specifically, never exchange "None" for "False". -- Nicola Larosa - [

Re: Default method arguments

2005-11-15 Thread Gregory Petrosyan
I want to find REALLY NICE AND PYTHONIC solution (if any) -- http://mail.python.org/mailman/listinfo/python-list

Re: Default method arguments

2005-11-15 Thread Bill Mill
On 11/15/05, Nicola Larosa <[EMAIL PROTECTED]> wrote: > > def f(self, x=None): > > if not x: > > Ha! You fell for it! ;-D > (Hint: what about x being passed with a value of zero? :-) ) I wasn't sure if you saw my post before you posted - good call. I just tossed off an answer without t

Default method arguments

2005-11-15 Thread gregory . petrosyan
Hello everybody! I have little problem: class A: def __init__(self, n): self.data = n def f(self, x = ) print x All I want is to make self.data the default argument for self.f(). (I want to use 'A' class as following : myA = A(5) myA.f() and get printed '5' as a resu

Re: Default method arguments

2005-11-15 Thread Duncan Booth
Nicola Larosa wrote: > # use new-style classes, if there's no cogent reason to do otherwise > class A(object): > def __init__(self, n): > self.data = n > def f(self, x = None) > # do NOT use "if not x" ! > if x is None: > print self.data > else: >

Re: Default method arguments

2005-11-15 Thread Nicola Larosa
> def f(self, x=None): > if not x: Ha! You fell for it! ;-D (Hint: what about x being passed with a value of zero? :-) ) > x = self.data > print x -- Nicola Larosa - [EMAIL PROTECTED] ...Linux security has been better than many rivals. However, even the best sys

Re: Default method arguments

2005-11-15 Thread Nicola Larosa
> I have little problem: > > class A: > def __init__(self, n): > self.data = n > def f(self, x = ) > print x > > All I want is to make self.data the default argument for self.f(). (I > want to use 'A' class as following : > > myA = A(5) > myA.f() > > and get printed

Re: Default method arguments

2005-11-15 Thread Bill Mill
On 15 Nov 2005 08:03:26 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hello everybody! > I have little problem: > > class A: > def __init__(self, n): > self.data = n > def f(self, x = ) > print x > > All I want is to make self.data the default argument for self.f