Re: default argument value is mutable

2016-10-07 Thread Marko Rauhamaa
"D'Arcy J.M. Cain" : > On Fri, 07 Oct 2016 16:09:19 +0200 > jmp wrote: >> What about >> >> def test(): >>if not hasattr(test, '_store'): test._store={'x':0} >>test._store['x'] += 1 > > Why is everyone working so hard to avoid creating a class? Hear, hear! Marko -- https://mail.python

Re: default argument value is mutable

2016-10-07 Thread ast
"jmp" a écrit dans le message de news:[email protected]... On 10/07/2016 03:45 PM, ast wrote: "jmp" a écrit dans le message de news:[email protected]... On 10/07/2016 02:07 PM, ast wrote: "jmp" a écrit dans le message d

Re: default argument value is mutable

2016-10-07 Thread D'Arcy J.M. Cain
On Fri, 07 Oct 2016 16:09:19 +0200 jmp wrote: > What about > > def test(): >if not hasattr(test, '_store'): test._store={'x':0} >test._store['x'] += 1 Why is everyone working so hard to avoid creating a class? -- D'Arcy J.M. Cain System Administrator, Vex.Net http://www.Vex.Net/ IM:da.

Re: default argument value is mutable

2016-10-07 Thread jmp
On 10/07/2016 03:45 PM, ast wrote: "jmp" a écrit dans le message de news:[email protected]... On 10/07/2016 02:07 PM, ast wrote: "jmp" a écrit dans le message de news:[email protected]... On 10/07/2016 01:38 PM, Daiyue Wen

Re: default argument value is mutable

2016-10-07 Thread ast
"jmp" a écrit dans le message de news:[email protected]... On 10/07/2016 02:07 PM, ast wrote: "jmp" a écrit dans le message de news:[email protected]... On 10/07/2016 01:38 PM, Daiyue Weng wrote: So the rule of thumb f

Re: default argument value is mutable

2016-10-07 Thread Steve D'Aprano
On Fri, 7 Oct 2016 11:48 pm, jmp wrote: > On 10/07/2016 02:07 PM, ast wrote: >> It can be used to store some variables from one call of >> a function to an other one. >> >> def test( _store={'x':0}): >> >> x = _store['x'] >> . do some stuff >>_store['x'] = x > > For personal dirt

Re: default argument value is mutable

2016-10-07 Thread Steve D'Aprano
On Fri, 7 Oct 2016 10:38 pm, Daiyue Weng wrote: > Hi, I declare two parameters for a function with default values [], > > def one_function(arg, arg1=[], arg2=[]): > > PyCharm warns me: > > Default argument value is mutable, > > what does it mean? and how to fix it? The usual way to avoid tha

Re: default argument value is mutable

2016-10-07 Thread jmp
On 10/07/2016 02:07 PM, ast wrote: "jmp" a écrit dans le message de news:[email protected]... On 10/07/2016 01:38 PM, Daiyue Weng wrote: So the rule of thumb for default argument value is "No mutable" Cheers, It can be used to store some variables from

Re: default argument value is mutable

2016-10-07 Thread ast
"jmp" a écrit dans le message de news:[email protected]... On 10/07/2016 01:38 PM, Daiyue Weng wrote: So the rule of thumb for default argument value is "No mutable" Cheers, It can be used to store some variables from one call of a function to an other

Re: default argument value is mutable

2016-10-07 Thread ast
"Daiyue Weng" a écrit dans le message de news:[email protected]... Hi, I declare two parameters for a function with default values [], def one_function(arg, arg1=[], arg2=[]): PyCharm warns me: Default argument value is mutable, what does it mean? and how

Re: default argument value is mutable

2016-10-07 Thread jmp
On 10/07/2016 01:38 PM, Daiyue Weng wrote: Hi, I declare two parameters for a function with default values [], def one_function(arg, arg1=[], arg2=[]): PyCharm warns me: Default argument value is mutable, what does it mean? and how to fix it? cheers You'll run into this bug def foo(a=[])

Re: default argument in method

2010-12-31 Thread DevPlayer
I agree with you Steven that the OP should avoid __getattribute__ and the like for many a thing. I also agree with your last statement. I try to answer the OP's question without much "You shouldn't do this's and don't do that's". I trust them to make thier own decisions. I'd say "A much better solu

Re: default argument in method

2010-12-30 Thread Steven D'Aprano
On Thu, 30 Dec 2010 11:26:50 -0800, DevPlayer wrote: > There's some_object.some_method.func_defaults Not quite -- method objects don't expose the function attributes directly. You need some_object.some_method.im_func to get the function object, which then has a func_defaults attribute. > and

Re: default argument in method

2010-12-30 Thread DevPlayer
There's some_object.some_method.func_defaults and some_function.func_defaults both are a settable attribute. How to set the methods func_defaults? You'd have to have code in _getattribute__(yourmethod) if not __getattr__(yourmethod) def __getattribute__(self, attr): if attr == self.my_method:

Re: default argument in method

2010-12-15 Thread Hans-Peter Jansen
On Thursday 16 December 2010, 00:56:31 Steven D'Aprano wrote: > On Wed, 15 Dec 2010 21:10:05 +0100, Hans-Peter Jansen wrote: > > Since this is a major pitfall, it might be worth mentioning, that > > mutable default arguments are generally a bad idea, as the default > > arguments are evaluated just

Re: default argument in method

2010-12-15 Thread Steven D'Aprano
On Wed, 15 Dec 2010 21:10:05 +0100, Hans-Peter Jansen wrote: > Since this is a major pitfall, it might be worth mentioning, that > mutable default arguments are generally a bad idea, as the default > arguments are evaluated just once, hence e.g. using an empty list might > contain the items, that

Re: default argument in method

2010-12-15 Thread Hans-Peter Jansen
On Monday 13 December 2010, 18:14:27 Godson Gera wrote: > On Sun, Dec 12, 2010 at 5:05 PM, ernest wrote: > > Hi, > > > > I'd like to have a reference to an instance attribute as > > default argument in a method. It doesn't work because > > "self" is not defined at the time the method signature is

Re: default argument in method

2010-12-13 Thread Steve Holden
On 12/13/2010 12:14 PM, Godson Gera wrote: > > > On Sun, Dec 12, 2010 at 5:05 PM, ernest > wrote: > > Hi, > > I'd like to have a reference to an instance attribute as > default argument in a method. It doesn't work because > "self" is not defined at th

Re: default argument in method

2010-12-13 Thread Godson Gera
On Sun, Dec 12, 2010 at 5:05 PM, ernest wrote: > Hi, > > I'd like to have a reference to an instance attribute as > default argument in a method. It doesn't work because > "self" is not defined at the time the method signature is > evaluated. For example: > > class C(object): >def __init__(se

Re: default argument in method

2010-12-13 Thread Jean-Michel Pichavant
ernest wrote: Hi, I'd like to have a reference to an instance attribute as default argument in a method. It doesn't work because "self" is not defined at the time the method signature is evaluated. For example: class C(object): def __init__(self): self.foo = 5 def m(self, val=se

Re: default argument in method

2010-12-12 Thread Chris Rebert
On Sun, Dec 12, 2010 at 3:35 AM, ernest wrote: > Hi, > > I'd like to have a reference to an instance attribute as > default argument in a method. It doesn't work because > "self" is not defined at the time the method signature is > evaluated. For example: > > class C(object): >    def __init__(sel

Re: default argument

2010-05-11 Thread Terry Reedy
On 5/11/2010 3:41 PM, Back9 wrote: self._value will be instance variable Then set it in the __init__ method. Read the tutorial and ref manual on Python class statements, which are a bit different from what you might be used to. -- http://mail.python.org/mailman/listinfo/python-list

Re: default argument

2010-05-11 Thread Dave Angel
Back9 wrote: On May 11, 3:20 pm, Chris Rebert wrote: On Tue, May 11, 2010 at 12:08 PM, Back9 wrote: On May 11, 3:06 pm, Back9 wrote: When i try it, it complains about undefined self. i don't know why. TIA Sorry here is the what i meant cla

Re: default argument

2010-05-11 Thread j vickroy
Back9 wrote: Hi, Is this grammer working in Python? class test: self._value = 10 def func(self, self._value) When i try it, it complains about undefined self. i don't know why. TIA ... not exactly; try: class Test: _value = 10 def func(self): print id(self._value), self._v

Re: default argument

2010-05-11 Thread Chris Rebert
On Tue, May 11, 2010 at 12:41 PM, Back9 wrote: > On May 11, 3:20 pm, Chris Rebert wrote: >> On Tue, May 11, 2010 at 12:08 PM, Back9 wrote: >> > On May 11, 3:06 pm, Back9 wrote: >> >> >> When i try it, it complains about undefined self. >> >> >> i don't know why. >> >> >> TIA >> >> > Sorry >> >

Re: default argument

2010-05-11 Thread Back9
On May 11, 3:20 pm, Chris Rebert wrote: > On Tue, May 11, 2010 at 12:08 PM, Back9 wrote: > > On May 11, 3:06 pm, Back9 wrote: > > >> When i try it, it complains about undefined self. > > >> i don't know why. > > >> TIA > > > Sorry > > here is the what i meant > > class test: > >  self._value =

Re: default argument

2010-05-11 Thread Chris Rebert
On Tue, May 11, 2010 at 12:08 PM, Back9 wrote: > On May 11, 3:06 pm, Back9 wrote: >> When i try it, it complains about undefined self. >> >> i don't know why. >> >> TIA > > Sorry > here is the what i meant > class test: >  self._value = 10 >  def func(self, pos = self._value) You're still defin

Re: default argument

2010-05-11 Thread Back9
On May 11, 3:06 pm, Back9 wrote: > Hi, > > Is this grammer working in Python? > > class test: >   self._value = 10 >   def func(self, self._value) > > When i try it, it complains about undefined self. > > i don't know why. > > TIA Sorry here is the what i meant class test: self._value = 10 de

Re: Default Argument Question

2008-10-29 Thread Benjamin Kaplan
On Wed, Oct 29, 2008 at 12:44 PM, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > Chris Rebert a écrit : > (snip) > >> Note that the "accumulation" behavior of lists is considered an >> aberration >> > > By who ? > All the python newbies who don't read the tutorial and get tripped up by this.

Re: Default Argument Question

2008-10-29 Thread Bruno Desthuilliers
Chris Rebert a écrit : (snip) Note that the "accumulation" behavior of lists is considered an aberration By who ? -- http://mail.python.org/mailman/listinfo/python-list

Re: Default Argument Question

2008-10-29 Thread Bruno Desthuilliers
Paulo J. Matos a écrit : Hi all, Going through the tutorial brought up a question. Consider the functions: def f(a, L=[]): L.append(a) return L print f(3) print f(9) print f(7) def f1(i = 0): i = i + 1 print i f1() f1() f1() f1() Since the f accumulates the values in L, I wa

Re: Default Argument Question

2008-10-29 Thread Chris Rebert
On Wed, Oct 29, 2008 at 9:14 AM, Paulo J. Matos <[EMAIL PROTECTED]> wrote: > Hi all, > > Going through the tutorial brought up a question. Consider the functions: > > def f(a, L=[]): >L.append(a) >return L > > print f(3) > print f(9) > print f(7) > > def f1(i = 0): >i = i + 1 >print

Re: default argument values qns

2006-06-01 Thread Alexandre Fayolle
Le 01-06-2006, [EMAIL PROTECTED] <[EMAIL PROTECTED]> nous disait: > hi > i have declared a function like this: > > def aFunction ( arg1 , arg2 = 0): > > print type(arg2) > > when i try to print the type of arg2, it gives me 'str' type..why is it > not integer type, since i ha

Re: default argument values qns

2006-06-01 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: > i have declared a function like this: > > def aFunction ( arg1 , arg2 = 0): > > print type(arg2) > > when i try to print the type of arg2, it gives me 'str' type..why is it > not integer type, since i have declared it as 0 ?? because you or someone

Re: default argument values qns

2006-06-01 Thread Tim Chase
> i have declared a function like this: > > def aFunction ( arg1 , arg2 = 0): > > print type(arg2) > > when i try to print the type of arg2, it gives me 'str' > type..why is it not integer type, since i have declared > it as 0 ?? >>> def a(arg1, arg2=0): ... print

Re: Default argument to __init__

2005-10-10 Thread Fredrik Lundh
"[EMAIL PROTECTED]" wrote: > Here's a piece of Python code and it's output. The output that Python > shows is not as per my expectation. Hope someone can explain to me this > behaviour: /snip/ > Why do myobj1.myarr and myobj2.myarr point to the same list? The > default value to __init__ for th

Re: Default argument to __init__

2005-10-10 Thread Larry Bates
This comes up on the list about once a week on this list. See: http://www.nexedi.org/sections/education/python/tips_and_tricks/python_and_mutable_n/view -Larry Bates [EMAIL PROTECTED] wrote: > Hi All: > > Here's a piece of Python code and it's output. The output that Python > shows is not as pe

Re: Default argument to __init__

2005-10-10 Thread Steve Holden
[EMAIL PROTECTED] wrote: > Hi All: > > Here's a piece of Python code and it's output. The output that Python > shows is not as per my expectation. Hope someone can explain to me this > behaviour: > > [code] > class MyClass: > def __init__(self, myarr=[]): > self.myarr = my

Re: Default argument to __init__

2005-10-10 Thread skip
vaibhav> Here's a piece of Python code and it's output. The output that vaibhav> Python shows is not as per my expectation. Hope someone can vaibhav> explain to me this behaviour: ... Yes, your default arg is evaluated once, at method definition time and shared betwee all instance