converting a string to a function parameter

Gary Herron gary.herron at islandtraining.com
Sun Jan 5 15:09:52 EST 2014


On 01/05/2014 11:39 AM, pietrodcof at gmail.com wrote:
> Il giorno venerdì 13 marzo 2009 08:52:39 UTC+1, koranthala ha scritto:
>> Hi,
>>      Is it possible to convert a string to a function parameter?
>> Ex:
>> str = 'True, type=rect, sizes=[3, 4]'
>> and I should be able to use it as:
>> test(convert(str)) and the behaviour should be same as calling test
>> with those values :
>> i.e. test(True, type=rect, sizes=[3, 4])
>>
>> I tried eval, but it did not work. And any other mechanism I think
>> turns out to be creating a full fledged python parser.
>>
>> Is there any mechanism with which we can do this straight away?
> I need the exact opposite, what is the inverse function?
> example: i pass to a function an argument
>
> m=[654,54,65]
> def function(m):
>      return takethenameof(m)
>
> and it have to return to me 'm' not [654,54,65] or '[654,54,65]'
>
> anybody can help?
> i think that when one is talking about a function he have to talk also of the inverse function (also because google have problems searching about this...

Absolutely not.  Objects (like [654,54,65]) do not have names, never did 
and never will!  Objects do have a type and a value (and an identity), 
but not a name.

Various namespaces will have dictionary-like associations between a name 
(like "m") and an object, and it *is* possible to get your hands on a 
(dictionary representing a) namespace and search it, but this is 
troublesome.

For instance, consider this small variation of your code:

def function(m):
    return takethenameof(m)

a=[654,54,65]
b = a
function(a)

While function is running, there will be three names associated with the list object.
The outer namespace will have "a" and "b" associated with the list object,
and the namespace local to function will have "m" associated with the same object.
That's one object associated with three names in two different namespaces.

Gary Herron





More information about the Python-list mailing list