KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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 .
Tags (comma-separated)
Save Edits
Cancel