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

The attribute's action of an object is not correct. #49870

Closed
Yongyang mannequin opened this issue Mar 31, 2009 · 3 comments
Closed

The attribute's action of an object is not correct. #49870

Yongyang mannequin opened this issue Mar 31, 2009 · 3 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@Yongyang
Copy link
Mannequin

Yongyang mannequin commented Mar 31, 2009

BPO 5620

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 2009-04-01.13:05:28.941>
created_at = <Date 2009-03-31.11:27:23.882>
labels = ['type-bug']
title = "The attribute's action of an object is not correct."
updated_at = <Date 2009-04-01.13:05:28.917>
user = 'https://bugs.python.org/Yongyang'

bugs.python.org fields:

activity = <Date 2009-04-01.13:05:28.917>
actor = 'Yong yang'
assignee = 'none'
closed = True
closed_date = <Date 2009-04-01.13:05:28.941>
closer = 'Yong yang'
components = ['None']
creation = <Date 2009-03-31.11:27:23.882>
creator = 'Yong yang'
dependencies = []
files = []
hgrepos = []
issue_num = 5620
keywords = []
message_count = 3.0
messages = ['84765', '84791', '85003']
nosy_count = 2.0
nosy_names = ['Kozyarchuk', 'Yong yang']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue5620'
versions = ['Python 3.0']

@Yongyang
Copy link
Mannequin Author

Yongyang mannequin commented Mar 31, 2009

The following is the test code.

class C1:
	myurl = []	
	def test(self):
		url = [5,6,7]
		self.myurl.extend(url)		
def testv():
	c = C1()
	c.test()
	print(c.myurl)
	
i = 0
while i<10 :
	testv()
	i = i+1

The output is :

[5, 6, 7]
[5, 6, 7, 5, 6, 7]
[5, 6, 7, 5, 6, 7, 5, 6, 7]
[5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7]
[5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7]
[5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7]
[5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7]
[5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6,
7]
[5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6,
7, 5, 6, 7]
[5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6, 7, 5, 6,
7, 5, 6, 7, 5, 6, 7]

The myurl of class C1 is not set to [] when a new object is created.
All objects use the same memory.

@Yongyang Yongyang mannequin added the type-bug An unexpected behavior, bug, or error label Mar 31, 2009
@Kozyarchuk
Copy link
Mannequin

Kozyarchuk mannequin commented Mar 31, 2009

AFAIK, This is expected behavior. myurl is a class attribute if you
want it to be different per instance you should re-initialize it in the
__init__ method. See below.

>>> class C1(object):
...     def __init__(self):
...             self.myurl = []
...     def test(self):
...             self.myurl.extend([5,6,7])
...
[44085 refs]
>>> def testv():
...     c = C1()
...     c.test()
...     print(c.myurl)
...
[44108 refs]
>>> for i in range(10):
...     testv()
...
[5, 6, 7]
[5, 6, 7]
[5, 6, 7]
[5, 6, 7]
[5, 6, 7]
[5, 6, 7]
[5, 6, 7]
[5, 6, 7]
[5, 6, 7]
[5, 6, 7]
[44119 refs]

@Yongyang
Copy link
Mannequin Author

Yongyang mannequin commented Apr 1, 2009

Thanks a lot!
Kozyarchuk.

I have thought the self.myurl should be the object variable, not class
variable.
The class variable really is confusing.

Why do they like that?

@Yongyang Yongyang mannequin closed this as completed Apr 1, 2009
@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
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

0 participants