class
of interestThis is the class
you want to “extend” / where you want to add the method.
Example - A class you already created:
# Python
class BoringClass:
"""
This class currently does nothing
"""
pass
Example - A class created by someone else:
# Python
from pandas import DataFrame
Note, as with any normal method, you will need to include self
as the first input to the function
# Python
def say_hello(self, n=2):
for _ in range(n):
print("Hello world!")
class
of interestExample - A class you already created:
# Python
BoringClass.say_hello = say_hello
Example - A class created by someone else:
# Python
DataFrame.say_hello = say_hello
Example - A class you already created:
# Python
bc = BoringClass()
bc.say_hello(n=1)
Hello world!
Example - A class created by someone else:
# Python
df = DataFrame([])
df.say_hello(n=3)
Hello world!
Hello world!
Hello world!