In Python, can you change how a method from class 1 acts on class 2 from within class 2?

Chris Stevens

Basically I have a class which subclasses ndarray and has additional information. When I call np.asarray() on my object, it returns just the numpy array and destroys my additional information.

My question is then this: Is there a way in Python to change how np.asarray() acts on my subclass of ndarray from within my subclass? I don't want to change numpy of course, and I do not want to go through every instance where np.asarray() is called to take care of this.

Thanks in advance!

Chris

cg909

Short answer: No. Numpy's asarray() doesn't check e.g. if a special method on the class of its argument exists and so doesn't provide a way to override its behaviour.

Long answer: It's not possible from your subclass, but you can hotpatch the numpy module in your module level code to replace the asarray function with your own wrapper. This is a very hacky solution and I don't recommend it, but it may work for you.

_real_asarray = np.asarray
def _new_asarray(a, dtype=None, order=None):
  if isinstance(a, MyClass):
    # special handling here
  else:
    return _real_asarray(a, dtype, order)
np.asarray = _new_asarray

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to change a variable from within a method of another class Java

How Can I Call a Class Method From a Different Class in Python?

How can i make a method available only from within the class

Can you "force" a method into a class from outside?

How to call a method of a 3rd class by calling the method of the 2nd class from the 1st class

Can you get/change data of ALL instances of an object from within a shared function within a class?

how to access a python method within a class from a staticmethod within the same class

How can a child class inherit a class method from its parent that gets a class variable from the child in python?

How do I call a base class method from within the same overloaded derived class method in python?

How can I access a class data member from a method within the same class?

Can I reference a class from a method within an inner class, and if so, how?

How would you the "this" pointer of the base class from a method of a nested class?

How do you call a method in one class, from another class?

How do you disconnect from a boost signal2 using a functor to a method of a class?

Yii2 access class from within the class

Can you override a function from a class outside of a class in Python

MATLAB How to modify property of a value class from a method within a class that is within the original class?

Can you run a java class from within a java program?

how to access a class attribute from within the class in Python

Using method from Class to change class

Python refer to class from within class

How to pass an instance of of a class from within that class

How to properly call a class method from within another method

Grails / Spock: How to mock single method within class where method is called from within the class itself?

How can you access an object from a different class into one class in Python?

How can I change the values of a class from another class?

Can we call a method from within a class of no instantiation

How can I use a Method from a class in a different class?

How to call get method from a class in Angular 2