Re: [Python-Dev] Possible bug in class-init, lookin for mentors

2017-04-21 Thread Antoine Rozo
And it is not related to __init__ method.
You have the same behaviour with any other function or method.

>>> def append_to_list(item, l=[]):
... l.append(item)
... return l
...
>>> append_to_list(1)
[1]
>>> append_to_list(2)
[1, 2]

2017-04-21 17:18 GMT+02:00 Manolis Mavrofidis :

> In a nutshell.
> You create two instances and you assign the same list to both of them
> which you instantiate when you run your code.
> >>> id(spam_1.list)
> 4530230984 # <- Here
> >>> id(spam_2.list)
> 4530230984 # <- Here
> >>> id(spam_1)
> 4530231632 # Nice unique instance
> >>> id(spam_2)
> 4530231200 # Nice unique instance as well
>
> Try
> >>> class Foo:
> ... def __init__(self, list=None):
> ... self.list = list
> ...
> >>> spam_1 = Foo()
> >>> spam_2 = Foo([]) <- Cheating.
> >>> spam_1
> <__main__.Foo instance at 0x10e05d9e0>
> >>> spam_2
> <__main__.Foo instance at 0x10e05d950>
> >>> spam_2.list.append(42)
> >>> print(spam_1.list)
> None
> >>> print(spam_2.list)
> [42]
> >>> id(spam_1.list)
> 4527705752
> >>> id(spam_2.list)
> 4530231488
>
> Or something along those lines :)
>
> On 21 April 2017 at 16:03, Guyzmo via Python-Dev 
> wrote:
> > On Fri, Apr 21, 2017 at 11:47:24AM +0200, Justus Schwabedal wrote:
> >> At least I think it's a bug.  Maybe it's a feature..
> >
> > it's indeed a feature.
> >
> >> I possibly found a bug in class __init__ and would like to fix it
> >
> > technically, it's a method. More precisely, it's the constructor method.
> >
> >> So I'm looking for a mentor to help me.
> >>
> >> class Foo:
> >> def __init__(self, bar=[]):
> >> self.list = bar
> >>
> >> spam_1 = Foo()
> >> spam_2 = Foo()
> >>
> >> spam_1.list.append(42)
> >> print(spam_2.list)`
> >
> > the argument `bar` of your method is instanciated at the time you're
> > declaring the method. It's happening once for the the lifetime of the
> > execution of your code.
> >
> > By allocating the `bar` reference into the `self.list` member, you're
> > assigning the same *instance* of that list into the `self.list` member.
> >
> > So everytime you create a new Foo instance, you're actually assigning
> > the same `[]` instance into `self.list` which is why, when you mutate
> > the list, it's happening in all the instances of Foo as well.
> >
> > I hope it makes sense to you !
> >
> > --
> > Guyzmo
> > ___
> > Python-Dev mailing list
> > Python-Dev@python.org
> > https://mail.python.org/mailman/listinfo/python-dev
> > Unsubscribe: https://mail.python.org/mailman/options/python-dev/
> mmavrofides%40gmail.com
>
>
>
> --
> "Only those who will risk going too far
> can possibly find out how far one can go.
>  "T.S. Eliot
> http://0x109.tuxfamily.org
> ___
> Python-Dev mailing list
> Python-Dev@python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
> antoine.rozo%40gmail.com
>



-- 
Antoine Rozo
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] Re: Accepting PEP 584: Add Union Operators To dict

2020-02-26 Thread Antoine Rozo
Maybe it's more the `a ||= b` that acts like a `a = a or b` in Python?
But then I don't think there is a confusion on Python side because
languages with a || operator usually already has a simple | with a
different meaning.

Le jeu. 27 févr. 2020 à 00:28, Nick Coghlan  a écrit :
>
>
>
> On Thu., 27 Feb. 2020, 2:03 am Guido van Rossum,  wrote:
>>
>> On Wed, Feb 26, 2020 at 7:43 AM Claudio Jolowicz  wrote:
>>>
>>> In my experience, the expression `value |= other` is a common idiom across
>>> programming languages to provide a default for `value` if it is "unset".
>>
>>
>> Interesting. Can you point to specific examples of this? In what other 
>> languages have you seen this? (Not that it would make us change PEP 584, but 
>> if this appears common we could probably warn about it prominently in docs 
>> and tutorials.)
>
>
> I was thinking that bash scripting might be an example, but I double checked, 
> and that's spelled 'VAR="${$VAR:-default value}" '
>
> make has 'VAR ?= "default value"'
>
> C# uses "??=" for null coalescence on assignment.
>
> So I've also never come across "|=" being used for this purpose.
>
> Cheers,
> Nick.
>
> ___
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-le...@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-dev@python.org/message/HMKYUZP5T6HTURG46GU3L72KANB65MLQ/
> Code of Conduct: http://python.org/psf/codeofconduct/



-- 
Antoine Rozo
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/73D2MVBXG6MAZ7KI76PKTEZPMMLQH6PQ/
Code of Conduct: http://python.org/psf/codeofconduct/