Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've read through the NetworkX tutorial and documentation, but was unable to find real world answers for this question.

Usually when using NetworkX, I might use strings to define nodes, then set several attributes.


G = nx.Graph()
G.add_node('John Doe', haircolor = 'brown')
G.node['John Doe'][age] = 22

However, it seems like declaring a class with members instead of attributes is better in practice, especially when there are many attributes and readability might be an issue.

class Person:
     name = None
     age = None

Person p
p.name = 'John Doe'
p.age = 22

Could someone be kind enough to validate my reasoning? I lack the foresight to see if Networkx node/edge attributes would be preferable.

share|improve this question
This may sound like a strange question, but are you coming from a Java background? If so, you may be fighting the urge to 'class everything', whereas python is taking you down another route. The first example works because python deals with lists efficiently, and the use of dictionaries (think: a fancy list made to look like a key-store) is common. Notice that it took fewer lines of code in the first example - this is a good sign you are "being pythonic". –  Avery Payne Feb 21 '12 at 23:23

1 Answer 1

up vote 7 down vote accepted

This doesn't answer your question. However. It seems necessary.

class Person:
     name = None
     age = None

Doesn't do what you're suggesting.

Those are two class-level attributes. They're emphatically not instance variables.

Also. You don't "declare" attributes at all. You don't declare them like that.

Person p isn't proper Python.

p= Person()
p.name = 'John Doe'
p.age = 22

This emphatically does not set the class level attributes created as part of the class. It creates additional instance-level attributes.

This may answer your question.

Networkx allows you to have any object associated with a node.

Feel free to do this

class Person( object ):
    def __init__( self, name, age ):
        self.name= name
        self.age= age

G.add_node('John Doe', data = Person( name='John Doe', age=22 ) )

Now you have all of your node data in a single object associated with the 'data' attribute.

For trivial name-value kinds of things, this is not obviously creating any real value.

But, if you have some node-specific method (rare in graph problems, but possible) then you'd have method functions associated with a node.

In graph theory problems, many of the algorithms work on the graph -- as a whole -- and you'll rarely find a use for a class with method functions.

Since the change is a trivial piece of syntax, it's probably simpler to start with

    G.add_node('John Doe', age=22 )

And migrate to

    G.add_node('John Doe', data = Person( name='John Doe', age=22 ) )

when you absolutely need to.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.