Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make python class support item assignment?

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.

like image 258
Jack Stout Avatar asked Dec 06 '12 03:12

Jack Stout


People also ask

Does set support item assignment Python?

The Python "TypeError: 'set' object does not support item assignment" occurs when we try to change the value of a specific item in a set. If you meant to declare a list, use square brackets instead of curly braces, e.g. my_list = [] . Here is an example of how the error occurs.

Which object does not support item assignment in Python?

The Python "TypeError: 'type' object does not support item assignment" occurs when we assign a data type to a variable and use square brackets to change an index or a key. To solve the error, set the variable to a mutable container object, e.g. my_list = [] .

Which data structures support item assignment in Python?

Python also supports user-defined data structures such as arrays, stacks, queues and so on.

Does set support item assignment?

How to Solve Python TypeError: 'set' object does not support item assignment. In Python, you cannot access the elements of sets using indexing. If you try to change a set in place using the indexing operator [], you will raise the TypeError: 'set' object does not support item assignment.


Video Answer


3 Answers

They are accomplishing this magic by inheriting from dict. A better way of doing this is to inherit from UserDict or the newer collections.MutableMapping

You could accomplish a similar result by doing the same:

import collections

class ObjectChild(collections.MutableMapping):
    def __init__(self, name):
        self['name'] = name

You can also define two special functions to make your class dictionary-like: __getitem__(self, key) and __setitem__(self, key, value). You can see an example of this at Dive Into Python - Special Class Methods.

like image 126
Corey D Avatar answered Oct 11 '22 02:10

Corey D


In Python 3 and later, just add these simple functions to your class:

class some_class(object):
    def __setitem__(self, key, value):
        setattr(self, key, value)

    def __getitem__(self, key):
        return getattr(self, key)
like image 39
Byron Broughten Avatar answered Oct 11 '22 02:10

Byron Broughten


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.

like image 34
Félix Cantournet Avatar answered Oct 11 '22 04:10

Félix Cantournet