Home > Back-end >  How do certain Python packages modify/add to methods of other packages?
How do certain Python packages modify/add to methods of other packages?

Time:12-17

For eg: Pandas provides a DataFrame object with the apply method. Installing another package Pandarallel provides the same pandas DataFrame object with a new parallel_apply method. How is this achieved?

CodePudding user response:

Most objects are mutable. It just adds more properties to the classes.

For example,

from collections import Counter
c = Counter('abc')
try:
   c.foo()  # AttributeError: 'Counter' object has no attribute 'foo'
except e:
   pass
c.foo = lambda: print('hello')
c.foo()  # prints hello

For a more explicit answer, look at the source code - https://github.com/nalepae/pandarallel/blob/master/pandarallel/pandarallel.py#L572

CodePudding user response:

Looks like if you add a method to a class, all objects created prior to this would also have the new method.

class A:
    pass

a = A()

A.m = lambda self, x: x

# This returns 5!!
a.m(5)

CodePudding user response:

While it doesn't appear to be the technique used in your referenced package, one way is that class objects can inherit from other class objects.

So in a separate package, you can create a new CustomDataFrame class:

from pandas import DataFrame
class CustomDataFrame(DataFrame):
"""Custom dataframe with new method"""
    def new_func(self, x):
        print(x)

df = CustomDataFrame()
df.new_func("new function that prints")  # --> prints "new function that prints"

CustomDataFrame will have all the attributes of the original base class DataFrame, plus the CustomDataFrame.new_func() method.

Naming the derived class with the same name as the base class is also possible, and new methods created with the same name will override the base class method.

from pandas import DataFrame
class DataFrame(DataFrame):
"""Custom dataframe with new method"""
    # methods of same name will override the base class
    def apply(self):
        print("df.apply() overridden")

df = DataFrame()
df.apply()  # --> prints "df.apply() overridden"
  • Related