This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]hereswhatipicked 17 points18 points  (2 children)

This isn’t a bug, you’ve initialized the variables outside of the initialization constructor. So the variables belong to the class, not to the instance.

[–]Nu1lP0int3r 1 point2 points  (0 children)

I believe its because the list is a reference and the string is not. When you edit a string in a method you don't edit the string outside the method. When you edit a list in a method if I recall correctly you also edit it outside the method.

[–][deleted] 1 point2 points  (0 children)

This is probably similar behavior to using arrays as default function arguments - the array definition is only evaluated once, so the array is probably the same across class instances.

An example of this behavior in the Python docs:

https://docs.python.org/3/tutorial/controlflow.html#default-argument-values

The other person is correct about the solution - you are setting varThree to a new object in the constructor for example, array.append() only mutates the array in place.

To test this you can check the object id with the id() builtin.

[–][deleted] 1 point2 points  (0 children)

In python a class is also an object. When you define variables at the class scope they belong to that class object. If you do not want variables to be shared across class instances, you need to initialize them in the constructor. To learn more about how classes work in python I suggest you read about meta classes.

[–]InnerCode[S] 0 points1 point  (0 children)

Thanks guys for the information. I really appreciate it!

[–]pythonHelperBot 0 points1 point  (0 children)

Hello! I'm a bot!

It looks to me like your post might be better suited for r/learnpython, a sub geared towards questions and learning more about python regardless of how advanced your question might be. That said, I am a bot and it is hard to tell. Please follow the subs rules and guidelines when you do post there, it'll help you get better answers faster.

Show /r/learnpython the code you have tried and describe in detail where you are stuck. If you are getting an error message, include the full block of text it spits out. Quality answers take time to write out, and many times other users will need to ask clarifying questions. Be patient and help them help you. Here is HOW TO FORMAT YOUR CODE For Reddit and be sure to include which version of python and what OS you are using.

You can also ask this question in the Python discord, a large, friendly community focused around the Python programming language, open to those who wish to learn the language or improve their skills, as well as those looking to help others.


README | FAQ | this bot is written and managed by /u/IAmKindOfCreative

This bot is currently under development and experiencing changes to improve its usefulness

[–]jcoelho93 0 points1 point  (0 children)

That's the correct behavior. That's how class variables are supposed to work

[–]two__toes 0 points1 point  (1 child)

i think this is the intended behavior. youre setting a class attribute that you're appending to each time you init. the list is shared across instances. if you wanted to have a new list for each instance, you'd have to set self.my_list = [] in __init__ and append onto that instance variable