Answer the question
In order to leave comments, you need to log in
How do shared references work in classes?
From one class I create two objects like this
class qw:
a = [1, 2]
x = qw()
y = qw()
print(x.a, y.a)
x.a = x.a.append(3)
print(x.a, y.a)
[1, 2] [1, 2]
None [1, 2, 3]
Answer the question
In order to leave comments, you need to log in
First you need to add an element to the list, then "assign". Since append is a method, xa gets its return value, a strongly typed language would throw an error because append doesn't return anything, but python is fine with "assigning". However , Roman Kitaev said everything correctly.
But if you want to write code in this way, I can please you, syntactic sugar := will appear in python 3.8, you can do this with it.
You didn't read the documentation, right? append added a triple to the list, which all class instances fumble, but append returns nothing. And it's nothing you assign
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question