Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1004347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:07:13+00:00 2026-05-16T08:07:13+00:00

I need to extend the Networkx python package and add a few methods to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:07:13+00:00Added an answer on May 16, 2026 at 8:07 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I need to extend the CListControl class in C++/MFC, which will add several
I need to extend a class, which is encapsulated in a closure. This base
I have the capability to extend a class at compile time, but I need
I need to add functionality to the close button in my class that extends
This is an external class I need to extend: public class Binary { public
I'm new to js and need to extend dom Elements with my custom methods
I need to extend a class (in C++, but this question is language-agnostic I
I need to extend a python code which has plenty of hard coded path
I need to extend the Magento shopping cart to include an extra step for
I have a college project where I need to extend an existing spreadsheet application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.