1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Strange behavior of lists as class attributes [duplicate]

Discussão em 'Python' iniciado por Stack, Setembro 14, 2025.

  1. Stack

    Stack Membro Participativo

    >>> class Abcd:
    ... a = ''
    ... menu = ['a', 'b', 'c']
    ...
    >>> a = Abcd()
    >>> b = Abcd()
    >>> a.a = 'a'
    >>> b.a = 'b'
    >>> a.a
    'a'
    >>> b.a
    'b'


    It's all correct and each object has own 'a', but...

    >>> a.menu.pop()
    'c'
    >>> a.menu
    ['a', 'b']
    >>> b.menu
    ['a', 'b']


    How could this happen? And how to use list as class attribute?

    Continue reading...

Compartilhe esta Página