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
Thanks Martin, you are right.
--
http://mail.python.org/mailman/listinfo/python-list
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
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_
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
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
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
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
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
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
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
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
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:
>
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
"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):
>
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
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
[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
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
Great thanks, Alex!
--
http://mail.python.org/mailman/listinfo/python-list
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
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
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:
>
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
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(
[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 :
>
> 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 - [
I want to find REALLY NICE AND PYTHONIC solution (if any)
--
http://mail.python.org/mailman/listinfo/python-list
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
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
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:
>
> 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
> 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
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
34 matches
Mail list logo