I need to extend the Networkx python package and add a few methods to the Graph
class for my particular need
The way I thought about doing this is simplying deriving a new class say NewGraph
, and adding the required methods.
However there are several other functions in networkx which create and return Graph
objects (e.g. generate a random graph). I now need to turn these Graph
objects into NewGraph
objects so that I can use my new methods.
What is the best way of doing this? Or should I be tackling the problem in a completely different manner?
__class__ # Output: <class 'type'> Thus, the above code shows that the Human class and every other class in Python are objects of the class type . This type is a class and is different from the type function that returns the type of object.
The class from which a class inherits is called the parent or superclass. A class which inherits from a superclass is called a subclass, also called heir class or child class.
Classes that inherit from another are called derived classes, subclasses, or subtypes. Classes from which other classes are derived are called base classes or super classes. A derived class is said to derive, inherit, or extend a base class.
If you are just adding behavior, and not depending on additional instance values, you can assign to the object's __class__
:
from math import pi class Circle(object): def __init__(self, radius): self.radius = radius def area(self): return pi * self.radius**2 class CirclePlus(Circle): def diameter(self): return self.radius*2 def circumference(self): return self.radius*2*pi c = Circle(10) print c.radius print c.area() print repr(c) c.__class__ = CirclePlus print c.diameter() print c.circumference() print repr(c)
Prints:
10 314.159265359 <__main__.Circle object at 0x00A0E270> 20 62.8318530718 <__main__.CirclePlus object at 0x00A0E270>
This is as close to a "cast" as you can get in Python, and like casting in C, it is not to be done without giving the matter some thought. I've posted a fairly limited example, but if you can stay within the constraints (just add behavior, no new instance vars), then this might help address your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With