Skip to content

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

Closed

Description

@Yongyang
mannequin
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']

Activity

  1. Yongyang commented on Mar 31, 2009

    Yongyangmannequin
    MannequinAuthor

    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.

  2. added
    type-bug
    on Mar 31, 2009
  3. Kozyarchuk commented on Mar 31, 2009

    Kozyarchukmannequin
    Mannequin

    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]
  4. Yongyang commented on Apr 1, 2009

    Yongyangmannequin
    MannequinAuthor

    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?

  5. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions