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.KeyField=KeyField
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.

Thanks

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.

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

First. USE CODE tag

Second.
You can use these functions setattr()

>>> class Test: pass

>>> t = Test()
>>> setattr(t, "foo", True) # Equivilent of t.foo = True
>>> t.foo
True

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.

Thanks!

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]):
slo_v_ang()
if(b.get()==j[0] and c.get()==j[2]):
slo_v_nem()
if(b.get()==j[1] and c.get()==j[0]):
ang_v_slo()
if(b.get()==j[1] and c.get()==j[2]):
ang_v_nem()
if(b.get()==j[2] and c.get()==j[0]):
nem_v_slo()
if(b.get()==j[2] and c.get()==j[1]):
nem_v_ang()

slo1=("ena","dva","tri","štiri","pet")
ang1=("one","two","three","four","five")
nem1=("eins","zwei","drei","vier","fünf")

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


okno=Tk()

okno.title('Slovar')

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

a=Entry(okno)
a.pack()

b=StringVar()
c=StringVar()

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

opti1.pack(fill=X)
opti2.pack(fill=X)

b.set('nem')
c.set('ang')

pret=Button(okno,text='Pretvori',command=pretvori)
pret.pack()

s=StringVar()
d=Label(okno,textvariable=s)
d.pack()

okno.mainloop()

@Valex, start your own thread.

Be a part of the DaniWeb community

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