Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
4 Antworten
16120 Ansichten

Hi All,

How to override method without class?

e.g

In a.py file have one method

def abc(self):
     print "Called ABC()"

 

In b.py i want to override a.py file's abc() method.

 

=================================
I clear about.

1) I am not talking about Class method override.
2) And no any class use in a.py and b.py
3) It is possible to override abc() method without class ?

 

Thanks in advance,
Harsh Dhaduk
 

Avatar
Verwerfen
Autor

PS :- Monkey Patch using hook https://youtu.be/8K1o9Xhk4gY

Beste Antwort

from a import abc

def new_method():

    ----

abc = new_method

OR

import a

def new_method():

    ----

a.abc = new_method

it's called monkey patching.  You can search the net for other examples, but here is one: http://stackoverflow.com/questions/19545982/monkey-patching-a-class-in-another-module-in-python

Avatar
Verwerfen

method abc is in module a

Good catch Ben. I've updated the answer.

Beste Antwort

Hi Harsh,

To override a method you have to understand the object to which that method belongs.

If method abc() belongs to class object 'test'

ie. in .py file it will be like:-

class test (osv.osv):

    _name = 'test'

    def abc():

        return True

 

If you want to override that function you have to define a class to inherit that object and then define method with same name. Eg:-

IN b.py file

class test(osv.osv):

    _inherit = 'test'

    def abc():

        return True

 

Here we overrided the method abc()

Avatar
Verwerfen

Thanks a lot!

Autor Beste Antwort

Hi Baiju i am not talking about class method override.

Hi Ivan have other examples like monkey patching?

Hi Bole i tried that one but not working.

 

Thanks for your replay.

Avatar
Verwerfen

Google python monkey patch and you can get a lot of examples.

Beste Antwort

try in b.py:

import a
def ab1(self...):
   something here

a.abc = ab1

 

Avatar
Verwerfen
Verknüpfte Beiträge Antworten Ansichten Aktivität
0
März 25
1250
4
Apr. 24
173936
0
Dez. 23
2021
5
Juli 25
227403
1
Dez. 22
3108