Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast base class to derived class python (or more pythonic way of extending classes)

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?

like image 849
zenna Avatar asked Aug 12 '10 01:08

zenna


People also ask

What is __ class __ in Python?

__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.

What is subclass and superclass in Python?

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.

What are derived classes in Python?

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.


1 Answers

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.

like image 134
PaulMcG Avatar answered Oct 14 '22 03:10

PaulMcG