Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63215 > unrolled thread
| Started by | pietrodcof@gmail.com |
|---|---|
| First post | 2014-01-05 11:39 -0800 |
| Last post | 2014-01-05 12:09 -0800 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: converting a string to a function parameter pietrodcof@gmail.com - 2014-01-05 11:39 -0800
Re: converting a string to a function parameter Ned Batchelder <ned@nedbatchelder.com> - 2014-01-05 14:58 -0500
Re: converting a string to a function parameter Gary Herron <gary.herron@islandtraining.com> - 2014-01-05 12:09 -0800
| From | pietrodcof@gmail.com |
|---|---|
| Date | 2014-01-05 11:39 -0800 |
| Subject | Re: converting a string to a function parameter |
| Message-ID | <ef4ee1e8-e247-4fda-ae5f-439dd9742a75@googlegroups.com> |
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...)
[toc] | [next] | [standalone]
| From | Ned Batchelder <ned@nedbatchelder.com> |
|---|---|
| Date | 2014-01-05 14:58 -0500 |
| Message-ID | <mailman.4963.1388951905.18130.python-list@python.org> |
| In reply to | #63215 |
On 1/5/14 2:39 PM, pietrodcof@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
[toc] | [prev] | [next] | [standalone]
| From | Gary Herron <gary.herron@islandtraining.com> |
|---|---|
| Date | 2014-01-05 12:09 -0800 |
| Message-ID | <mailman.4965.1388952599.18130.python-list@python.org> |
| In reply to | #63215 |
On 01/05/2014 11:39 AM, pietrodcof@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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web