converting a string to a function parameter

Ned Batchelder ned at nedbatchelder.com
Sun Jan 5 14:58:09 EST 2014


On 1/5/14 2:39 PM, 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...)
>

The difficulty in writing such a function is that values don't have 
unique names, if they have names at all.  What should be returned in 
these cases?

     m = [654, 54, 65]
     def function(m):
         m2 = m
         m3 = m[:]
         takethenameof(m)
         takethenameof(m2)
         takethenameof(m3)
         takethenameof(m[:])
         takethenameof(2)
         takethenameof(2+2)

There are samples online that try to do a "reasonable" job of this, but 
my googling isn't turning them up...

-- 
Ned Batchelder, http://nedbatchelder.com




More information about the Python-list mailing list