0

This is a conceptual problem, I need to instantiate a number of 'nodes' for a network simulation. The naming is the problem - I need a method to autogenerate names for the instances (any method that allows me to track and reference the nodes after creation will do)

to give a better idea of what I need, here's what most of the loop would look like with hashtags shoehorned into where the problem is. x and y will be defined in the parent function.

    for n in range(1000):
        #n, but somehow converted into the name of the dict# = {
            'address':n,
            'resistance':x,
            'special':[],
            'parent':y,
            'children':[],
            'adjnodes':[]
    }

Apologies for being a noob. I've searched high and low for the answer to this so if it's obvious then I'm misusing the lingo or something; in that case please let me know what lingo to use and I'll grab my hat and be on my way. Not being sarcastic, just prefer to be taught harshly when necessary. Thanks.

  • I don't have a whole lot to add, other than personal anecdote -- I DID THIS. Not this exact problem, but I had to poll ~100 machines and pull some file size info when we found a purge process wasn't running correctly. I used the name of the files I was checking as each variable, and the monkeywrenching and hackjob coding I had to do to make that method work was IMMENSE. I will no longer maintain that code -- if it needs serious updates, I will write it again from scratch, knowing what I know now. Follow the advice of your answerers: KEEP DATA OUT OF YOUR VARIABLES. – Adam Smith Dec 16 '13 at 19:36
  • And that's why PHP is superior to python : http://www.php.net/manual/en/language.variables.variable.php ( kidding, downvote for trolling not for cluelessness please :-) – vincent Dec 16 '13 at 22:13
  • @vincent: Nah, Tcl is superior to both. `eval` is the one true way to do everything, so you don't have to remember anything fancy or efficient or safe, because no such thing exists. :) – abarnert Dec 17 '13 at 18:57

2 Answers2

4

It sounds like you're trying to create these as a bunch of separate variables, and then store the name of the variable in the address field of each one.

Don't do that. See Keep data out of your variable names and Why you don't want to dynamically create variables for an explanation as to why. But the short version is that trying to do so is the only reason you're having a problem in the first place.

Why not just create a single list of all of the nodes? Then your address can just be an index into that list. For example:

nodes = []
for n in range(1000):
    nodes.append({
        'address':n,
        'resistance':x,
        'special':[],
        'parent':y,
        'children':[],
        'adjnodes':[]
    })

Or, if you plan to be adding and removing nodes as you go along, so list indices won't stay consistent, just use a dict:

nodes = {}
for n in range(1000):
    nodes[n] = {
        'address':n,
        'resistance':x,
        'special':[],
        'parent':y,
        'children':[],
        'adjnodes':[]
    }
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • was going to ask about this but found it elsewhere. Was wondering about using functions and classes particular to nodes. This guy had a super slick answer http://stackoverflow.com/questions/1208322/dictionary-with-classes shapes = {'1':Square, '2':Circle, '3':Triangle} # just the class names in the dict x = shapes[raw_input()]() # get class from dict, then call it to create a shape instance. – user3100457 Dec 17 '13 at 06:49
1

If n is meaningful, you can put each of those dicts into another dict with n as the key.

If n is not meaningful, just add those dicts to a list.

Either way, keep your data out of your variable names.

roippi
  • 25,533
  • 4
  • 48
  • 73