0

I'm not even sure if this is possible. What I'm trying to do is having an object call a method, which would reassign a new class object to the variable

Definition:

class A():
    def __init__(self):
        self.number = 1
    def becomeB(self):
        # a method that will assign this variable a class B object

class B():
    def __init__(self):
        self.name = "I am B"

What I'm trying to achieve:

myObj = A()
myObj.becomeB()
print(myObj.name)

Output: I am B

Dan
  • 195
  • 3
  • 12
  • 5
    Sounds like an [XY problem](http://xyproblem.info/). What is the bigger picture? Why do you need this? – a_guest Apr 16 '20 at 10:56
  • @a_guest honestly, no I'm not trying to do anything in particular. I just want to know if such a thing is possible – Dan Apr 16 '20 at 10:59
  • Not sure if it's a duplicate, but related: https://stackoverflow.com/questions/15404256/changing-the-class-of-a-python-object-casting – Tomerikoo Apr 16 '20 at 11:02
  • 1
    @Dan The problem is that you ask for very specific behavior. You can get the desired output by `def becomeB(self): B.__init__(self)` but most likely this isn't what you want. You also ask for *"a method that will assign this variable a class B object"*. You can do that via `self = B()` but this won't have any effect outside the scope of the function, so again this most likely isn't what you want. That's why it is important to understand the bigger picture, in order to give a precise answer. – a_guest Apr 16 '20 at 11:20

2 Answers2

2

It doesn't make any sense to convert object of type A to be of type B. Instead, I would recommend you to make becomeB a function that returns object B.

For example:

class B():
    def __init__(self):
        self.name = "I am B"


class A():
    def __init__(self):
        self.number = 1
    def becomeB(self):
        return B()

myObj = A()
b = myObj.becomeB()
print(b.name) # output: I am B
Gabio
  • 9,126
  • 3
  • 12
  • 32
  • I figure this does exactly what the requirements asks for, yes. But just out of curiosity, is there really a void method that will do so without having to perform the assignment? I'm not trying to solve a particular problem, I'm just asking out of pure curiosity – Dan Apr 16 '20 at 11:20
  • @Dan don't think so. All the python built-in methods for type conversion return new object (like `int("5")`) and not mutate the current. – Gabio Apr 16 '20 at 11:26
1

If you want an object to behave like an instance from another class you can reassign the __class__ attribute:

def becomeB(self):
    self.__class__ = B

Here's a more complete example:

class A:
    def __init__(self, name):
        self.name = name

    def foo(self):
        return f'A says: Hello {self.name}'

    def becomeB(self):
        self.__class__ = B


class B:
    def foo(self):
        return f'B says: Hello {self.name}'


a = A('World')
print(a.foo())  # prints: A says: Hello World
a.becomeB()
print(a.foo())  # prints: B says: Hello World
a_guest
  • 34,165
  • 12
  • 64
  • 118