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

New class instance not initializing variables of type list #79620

Closed
ArturoInzunza mannequin opened this issue Dec 8, 2018 · 2 comments
Closed

New class instance not initializing variables of type list #79620

ArturoInzunza mannequin opened this issue Dec 8, 2018 · 2 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@ArturoInzunza
Copy link
Mannequin

ArturoInzunza mannequin commented Dec 8, 2018

BPO 35439
Nosy @ammaraskar

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 2018-12-08.00:43:21.083>
created_at = <Date 2018-12-08.00:26:46.907>
labels = ['type-bug', 'invalid']
title = 'New class instance not initializing variables of type list'
updated_at = <Date 2018-12-08.00:43:21.068>
user = 'https://bugs.python.org/ArturoInzunza'

bugs.python.org fields:

activity = <Date 2018-12-08.00:43:21.068>
actor = 'ammar2'
assignee = 'none'
closed = True
closed_date = <Date 2018-12-08.00:43:21.083>
closer = 'ammar2'
components = []
creation = <Date 2018-12-08.00:26:46.907>
creator = 'Arturo Inzunza'
dependencies = []
files = []
hgrepos = []
issue_num = 35439
keywords = []
message_count = 2.0
messages = ['331370', '331371']
nosy_count = 2.0
nosy_names = ['ammar2', 'Arturo Inzunza']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue35439'
versions = ['Python 3.6']

@ArturoInzunza
Copy link
Mannequin Author

ArturoInzunza mannequin commented Dec 8, 2018

List type variables in a class are not reset on new instances of the class.

Example:

class Klazz:
    lst = []
    def __init__(self, va):
        print(self.lst)
        self.lst.append(va)


k = Klazz(1)
[]      -> This is correct as the lst value is empty on class instantiation

k2 = Klazz(2)
[1]   -> This is wrong, a totally new instance of the class retains the value of a previous class instance lst variable

k3 = Klazz(3)
[1, 2]  -> And so on... new instances all share the same list

@ammaraskar
Copy link
Member

This is expected behavior, take a look at this section in the tutorial on classes: https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables

Unlike say Java, member variables need to be initialized in the constructor.

@ammaraskar ammaraskar added invalid type-bug An unexpected behavior, bug, or error labels Dec 8, 2018
@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

1 participant