Get keys from a dicionary

Dave Angel d at davea.name
Fri Nov 11 11:47:21 EST 2011


On 11/11/2011 11:33 AM, macm wrote:
> Hi
>
> Sorry ! My mistake.
>
>>>> myDict = {}
>>>> myDict['foo'] = {}
>>>> myDict['foo']['bar'] = 'works'
> -----
>
>>>> def myFunction( MyObj ):
> ...	# MyObj is a nested dicionary (normaly 2 steps like myDict['foo']
> ['bar'])

No, it's not.   It's a string "works".  There's no dictionary passed to 
myFunction(), so it cannot do what you ask, slow or fast.

There are games you can play with introspection, but they are neither 
portable nor reliable.

> ...	# I want inspect this MyObj
> ...	# what keys was pass
> ...	print MyObj.keys() ## WRONG
> ...	# So What I want is :
> ...	# return foo bar
>
> ----------------
>
>>>> result = myFunction( myDict['foo']['bar'] )
>>>> result
> Should print :
>
> ... foo bar
>
> Best Regards
>
> macm
Can you tell us the exact assignment, to see whether this is supposed to 
be a practical question, or a way to try to learn more about Python 
internals.



-- 

DaveA




More information about the Python-list mailing list