Imagine having this class:
class Foo:
def __init__(self):
self.nr = 0
Imagine further Foo
being widely used. Several scripts and users use the variable Foo.nr
to read and write its value.
Now suppose the developer wants to change the name of .nr
to .val
.
class Foo:
def __init__(self):
self.val = 0
In order to not force the users to update their code, is it somehow possible to make them access .val
by writing .nr
? Like this:
f1 = Foo()
print(f1.nr)
I came up with properties and getter and setter methods but I don't see how they might help.
class Foo:
def __init__(self):
self.val = 0
@property
def nr(self):
return self.val
@nr.setter
def nr(self, value):
self.val = value
f1 = Foo()
print(f1.nr)
would give 0
as expected.