-2

MyClass and its instance are defined in myModule.py:

class MyClass(object):
    pass

obj = MyClass()

Define function as obj's method:

from myModule import obj
def function(msg):
    print msg 

How to extend MyClass instance with function as method? One way:

obj.function = function

But this won't be the same as if it would be defined in Class definition, e.g.:

class MyClass(object):
    def __init__(self): 
        self.value = 'Value'

    def function(self, msg):
        print msg, self.value

After function is defined as MyClass method it can access the Class's attributes such as self.value.

alphanumeric
  • 17,967
  • 64
  • 244
  • 392
  • Why are you unable to extend `MyClass`? Can you not define a new class that extends `MyClass`? – Ayush Jan 28 '17 at 00:05
  • You can get the class of an instance from its `__class__` attribute.This means you can use it as a base class for your own subclass. i.e. `class MyClass(instance.__class__):`. – martineau Jan 28 '17 at 02:26

2 Answers2

1

I have no idea why you're repeating this question as both wim and I provided solutions that don't involve subclassing.

When function() is defined inside of MyClass definition it is able to access the Class's attribute or variables such as self.value here....

I've expanded on my answer from your other post to demonstrate that this was already possible:

from unittest import mock

# This class would be defined in some third-party library
class MyClass:
    def __init__(self):
        self._private_property = 'foo'

    def method(self, msg):
        print('from method:', msg)


def function(self, msg):
    print('Accessing private property from function:', self._private_property)
    print('from function:', msg)


old_method = MyClass.method


def new_method(self, msg):
    old_method(self, msg)
    function(self, msg)


# The patch is only applied within this scope
with mock.patch.object(MyClass, 'method', new_method):
    foo = MyClass()
    foo.method('message with patched')

# By this point MyClass is "back to normal"
print('---')
foo.method('message with original')

Output

from method: message with patched
Accessing private property from function: foo
from function: message with patched
---
from method: message with original
Community
  • 1
  • 1
Tagc
  • 8,736
  • 7
  • 61
  • 114
  • Sorry, I was not clear enough. I am not able to access the Class definition here at all. I am only given Class instance. But please do not delete your answer since it is still a valuable resource and it is very close to the subject. – alphanumeric Jan 28 '17 at 00:32
  • @alphanumeric You can pass a string to `mock.patch.object` e.g. `with mock.patch.object('somemodule.MyClass', 'method', new_method)`. See [the docs](https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch). – Tagc Jan 28 '17 at 00:36
0
class MyClass(object):
    def __init__(self): 
        self.value = 'value'

def function(self, msg):
    print 'msg:', msg, 'value:', self.value 

foo = MyClass()
foo.function = function.__get__(foo)
foo.function(msg = 'hello world')
alphanumeric
  • 17,967
  • 64
  • 244
  • 392