I have two objects i sub-classed from the str and int classes, to which i add a revert method, which can turn one object to the other:
class newString(str):
...
...
def revert(self, distance, obj):
self = newInt(self)
class newInt(int):
...
...
def revert(self):
self = newString(self)
a = newInt(2)
a.revert()
print(type(a))
#output
<class "newString">
I know the "self" keyword can't be used this way but i put it like this for illustration. I want the object to be able to change itself to another, is this possible without having to use a return statement in the "revert" method? Because if i used a return statement it would mean I have to assign the the returned object back to a again and the syntax will be something like:
class newString(str):
...
...
def revert(self):
new = newInt(self)
return new
class newInt(int):
...
...
def revert(self):
new = newInt(self)
return new
a = newInt(2)
a = a.revert()
print(type(a))
#output
<class "newString">
which has always seemed a bit clumsy to me, thank you ;)
what I've tried: I've tried simulating passing by reference; example:
class newString(str):
...
...
def revert(self, obj):
obj[0] = newInt(obj[0])
class newInt(int):
...
...
def revert(self, obj):
obj[0] = newInt(obj[0])
a = newInt(2)
a.revert([a])
print(type(a))
#output
<class "newString">
but again, clumsy. since i have to wrap the variable in a list to make it mutable before passing it to the method. Any help is appreciated, thank you.