Can I link two variables such that one is double of another in python?

So basically I have two lists a and b and have defined them like a=b=[10] so changing one will change the other-

a=b=[10]
a[0]+=1
a
>>>[11]
b
>>>[11]

Is there a way to do this but instead make it give double of the variable? Desired output –

a=b=[10]
#some code
a
>>>[10]
b
>>>[20]

>Solution :

I’m not sure if I get what you’re trying to do correctly, but something like this should work.

prop = property(lambda self: self.a * 2)
# Dynamically creating a class with a default value for `a` and `b`
Class = type('ClassName', (object,), {"a": 0, "b": prop})

c = Class()
c.a = [10]
print(c.a, c.b)  # ([10], [10, 10])

Leave a Reply