[Python] Python add to a function dynamically

Discussão em 'Python' iniciado por Stack, Agosto 9, 2025.

  1. Stack

    Stack Membro Participativo

    how do i add code to an existing function, either before or after?

    for example, i have a class:

    class A(object):
    def test(self):
    print "here"


    how do i edit the class wit metaprogramming so that i do this

    class A(object):
    def test(self):
    print "here"

    print "and here"


    maybe some way of appending another function to test?

    add another function such as

    def test2(self):
    print "and here"


    and change the original to

    class A(object):
    def test(self):
    print "here"
    self.test2()


    is there a way to do this?

    Continue reading...

Compartilhe esta Página