[issue35439] New class instance not initializing variables of type list

2018-12-07 Thread Ammar Askar


Ammar Askar  added the comment:

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.

--
nosy: +ammar2
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed
type:  -> behavior

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35439] New class instance not initializing variables of type list

2018-12-07 Thread Arturo Inzunza


New submission from Arturo Inzunza :

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

--
messages: 331370
nosy: Arturo Inzunza
priority: normal
severity: normal
status: open
title: New class instance not initializing variables of type list
versions: Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com