I want to create a function or a class method that will create a dictionary named by a variable that is passed in.

By way of example, I'm creating a class method to read data from a table (in this case it's an ArcGIS table, but that's not critical) and I'd like the method to give me a dictionary identified by the string stored in OutputDictionaryName (see below).

class ReadTableIntoDictionary():

def __init__(self, TableSource, KeyField, OutputDictionaryName):
self.TableSource = TableSource
self.DictName = OutputDictionaryName

(rest of code goes here to extract the data from the table into a dictionary ... no problem, I can do that part)

But how do I create a dictionary called by the name in OutputDictionaryName?

For example I'd like to call
ReadTableToDictionary(PointCoordinateTable, PointID, PointCoordinateDict)

and get back a dictionary

PointCoordinateDict(k,v) that I can use

then I'd like to use the same method to read another table into a dictionary, eg

ReadTableToDictionary(DatumTable, DatumID, DatumDict)

I have several tables of data to read in and I'd like to just call this method for each table rather than having to write the same code over and over.

Seems simple, and it's probably easy, but it's Friday afternoon and I'm stumped.


Re: How do I create a Python Dictionary which is named by a variable? 80 80

How about defining dictionary of dictionaries and using the desired names as lookup from that table. Actually the variables of Python are defined that way, one table for globals and other for locals, if I have not understood wrong. See for example documentation of eval function.

Re: How do I create a Python Dictionary which is named by a variable? 80 80

Why will simply assigning the return value to a known variable not work in your case? Is there a unique reason you need to create a named dict inside the function?
Usually, you know what you are passing into the function, so it should be feasibile to assign the return value to a proper variable.

PointCoordinateDict = ReadTableToDictionary(PointCoordinateTable, PointID)
DatumDict = ReadTableToDictionary(DatumTable, DatumID)
commented: Well put. A blogger somewhere says don't create variable names at run-time in Python (dictionaries make that unnecessary) but I didn't know how to explain it so nicely. +1
Re: How do I create a Python Dictionary which is named by a variable? 80 80

First. USE CODE tag

You can use these functions setattr()

>>> class Test: pass

>>> t = Test()
>>> setattr(t, "foo", True) # Equivilent of t.foo = True
>>> t.foo
Re: How do I create a Python Dictionary which is named by a variable? 80 80

Thanks to all of you.

sergb's answer was the solution to my problem (it was the end of the day on Friday, I'm new to coding classes in Python, whatever other excuse I can think of for missing such a simple solution...)

I just joined DaniWeb and I was surprised by how fast I got feedback on my question.


Re: How do I create a Python Dictionary which is named by a variable? 80 80

Please help me!

I have to make dictionary in py tkinter and i don't know how to do definition

this is my code:

from Tkinter import *

j=("slo", "ang", "nem")

def pretvori():
if(b.get()==j[0] and c.get()==j[1]):
if(b.get()==j[0] and c.get()==j[2]):
if(b.get()==j[1] and c.get()==j[0]):
if(b.get()==j[1] and c.get()==j[2]):
if(b.get()==j[2] and c.get()==j[0]):
if(b.get()==j[2] and c.get()==j[1]):


def slo_v_ang():
if(a.get()==slo1[0] : s.set(str(ang1[0])))



oznaka=Label(okno, text='Vnesi besedo')



opti1=OptionMenu(okno, b, 'slo', 'ang', 'nem')
opti2=OptionMenu(okno, c, 'slo', 'ang', 'nem')






Re: How do I create a Python Dictionary which is named by a variable? 80 80

@Valex, start your own thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.