Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug in classlevel variabels #43914

Closed
thomasda mannequin opened this issue Aug 30, 2006 · 2 comments
Closed

bug in classlevel variabels #43914

thomasda mannequin opened this issue Aug 30, 2006 · 2 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@thomasda
Copy link
Mannequin

thomasda mannequin commented Aug 30, 2006

BPO 1549499
Nosy @birkenfeld

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2006-08-30.19:28:23.000>
created_at = <Date 2006-08-30.18:44:30.000>
labels = ['interpreter-core', 'invalid']
title = 'bug in classlevel variabels'
updated_at = <Date 2006-08-30.19:28:23.000>
user = 'https://bugs.python.org/thomasda'

bugs.python.org fields:

activity = <Date 2006-08-30.19:28:23.000>
actor = 'georg.brandl'
assignee = 'none'
closed = True
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2006-08-30.18:44:30.000>
creator = 'thomasda'
dependencies = []
files = []
hgrepos = []
issue_num = 1549499
keywords = []
message_count = 2.0
messages = ['29711', '29712']
nosy_count = 2.0
nosy_names = ['georg.brandl', 'thomasda']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1549499'
versions = ['Python 2.4']

@thomasda
Copy link
Mannequin Author

thomasda mannequin commented Aug 30, 2006

class A:
    js = []
    
    def add (self, j):
        self.js.append(j)

    def clone (self):
        c = A()
        for j in self.js:
            c.add(j)
        return c

a = A()
b = a.clone()
b.add(3)

print a.js
print b.js

The above code should print "[]\n[3]", but instead it
prints "[3]\n[3]"!

It works as expected, if you change "js = []" to "def
__init__ (self):
self.js = []"

@thomasda thomasda mannequin closed this as completed Aug 30, 2006
@thomasda thomasda mannequin added invalid interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Aug 30, 2006
@birkenfeld
Copy link
Member

Logged In: YES
user_id=849994

This is not a bug. The "js" list exists in the class's
namespace and exists only once, and if you don't rebind the
name, "self.js" always refers to this. Therefore it is
shared between instances.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

1 participant