Python: Is it possible to build an instance of a child class from an instance of the parent class?

It might be a terrible idea (feel free to tell me if that is the case), but I am exploring the boundaries of Python and I could see how this can be useful (in fact, I am looking at the potential thing for him right now).

Here is the setup:

--- (API File) ---

class APINode(object):
    def __init__(self, *args, **kwargs):
        # initialize some instance variables

    def render(self, arg1, arg2):
        # do some stuff and return something

def api_function( arg1, arg2 ):
    # do some stuff
    return APINode(args, kwargs)

---- (My file) ----

class MyNode(APINode):
    def render(self, arg1, arg2):
        #My override of APINode render 


def my_function( arg1, arg2 ):
    api_parent_instance = api_function( arg1, arg2 )
    #Can I some how create an instance of MyNode from api_parent_instance here?

api_function, . , : (1, yuck), api_function my_function, MyNode APINode (2, ?), api_function my_function, - APINode, - MyNode , .

: Python ?

( , ? Django.)

+5
6

, MyNode, APINode, . render() APINode. MyNode APINode.

. APINode, .

+6

.

>>> class A:
...     def hi(self):
...         print "I am an A!"
... 
>>> class B:
...     def hi(self):
...         print "I am a B!"
... 
>>> a = A()
>>> a.hi()
I am an A!
>>> # Doing this will likely lead to hard to find bugs.
>>> a.__class__ = B
>>> a.hi()
I am a B!
>>> 

API!

def render(self, arg1, arg2):
    #My override of APINode render 
APINode.render = render
#congratulations, now APINode uses your render function.

, .

+2

.

class B(object):
  def __new__(cls, val):
    if val:
      return D()
    return super(B, cls).__new__(cls)

  def foo(self):
    return 'bar'

class D(object):
  def foo(self):
    return 'baz'

b1 = B(False)
b2 = B(True)
print b1.foo()
print b2.foo()
+1

, , :

>>> class Monkey(object):
...     def eat(self, smth):
...             if self.likes(smth):
...                     print "Yummy!"
...             else:
...                     print "Yuck!"
...     def likes(self, smth):
...             return smth == "banana"
... 
>>> m = Monkey()
>>> m.eat("banana")
Yummy!
>>> m.eat("cheezburger")
Yuck!
>>> def patch(self, smth):
...     return True
...
>>> m.likes = type(m.likes)(patch, m.likes, Monkey)
>>> m.eat("cheezburger")
Yummy!
>>> 

:

def my_render(self, arg1, arg2):
    #My override of APINode render 

def my_function( arg1, arg2 ):
    api_parent_instance = api_function( arg1, arg2 )
    api_parent_instance.render = type(api_parent_instance.render)(
        api_parent_instance,
        api_parent_instance.render,
        api_parent_instance.__class__)

    ...
+1

, , - . : a = obj. _class _() , python .

, . obj. _class _() .

0

:

class Parent(object):
    def __init__(self):
        pass

    def generate_new_child_object(self):
        print "Generating a new object of type " + str(type(self))
        return type(self)()

class Child(Parent):
    pass

:

>>> child = Child()
>>> type(child)
<class 'Child'>
>>> generated_child = child.generate_new_child_object()
Generating a new object of type <class 'Child'>
>>> type(generated_child)
<class 'Child'>
0

All Articles