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 8950725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:30:15+00:00 2026-06-15T13:30:15+00:00

While looking over some code in Think Complexity , I noticed their Graph class

  • 0

While looking over some code in Think Complexity, I noticed their Graph class assigning values to itself. I’ve copied a few important lines from that class and written an example class, ObjectChild, that fails at this behavior.

class Graph(dict):
    def __init__(self, vs=[], es=[]):
        for v in vs:
            self.add_vertex(v)

        for e in es:
            self.add_edge(e)

    def add_edge(self, e):
        v, w = e
        self[v][w] = e
        self[w][v] = e

    def add_vertex(self, v):
        self[v] = {}

class ObjectChild(object):
    def __init__(self, name):
        self['name'] = name

I’m sure the different built in types all have their own way of using this, but I’m not sure whether this is something I should try to build into my classes. Is it possible, and how? Is this something I shouldn’t bother with, relying instead on simple composition, e.g. self.l = [1, 2, 3]? Should it be avoided outside built in types?

I ask because I was told “You should almost never inherit from the builtin python collections”; advice I’m hesitant to restrict myself to.

To clarify, I know that ObjectChild won’t “work”, and I could easily make it “work”, but I’m curious about the inner workings of these built in types that makes their interface different from a child of object.

  • 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-06-15T13:30:17+00:00Added an answer on June 15, 2026 at 1:30 pm

    Disclaimer : I might be wrong.

    the notation :

    self[something]
    

    is legit in the Graph class because it inherits fro dict. This notation is from the dictionnaries ssyntax not from the class attribute declaration syntax.

    Although all namespaces associated with a class are dictionnaries, in your class ChildObject, self isn’t a dictionnary. Therefore you can’t use that syntax.

    Otoh, in your class Graph, self IS a dictionnary, since it is a graph, and all graphs are dictionnaries because they inherit from dict.

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

Sidebar

Related Questions

While looking over some Ruby code I noticed methods declared with self. prepended to
While looking over some code, I found a function that seems to do exactly
while looking at some code I stumbled onto: throw /*-->*/new std::exception (//... and I
While looking through some old code I came across this gem: MyObject o =
Looking over some of the inputs for the GSM kit for XNA, I've noticed
I have been looking over the internet for a while about this, but it
While looking at some conceptual questions in C,I came across this question in a
I have been looking into refactoring some old code into a new WCF service,
I am attempting a quick prototype-port of some old database code over to use
While looking into parallel programming, and subsequently evaluation strategies, the question whether thunks are

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.