Re: Symbols as parameters?

2010-01-29 Thread Roald de Vries
On Jan 29, 2010, at 2:30 AM, Steven D'Aprano wrote: On Thu, 28 Jan 2010 17:01:38 +0100, Roald de Vries wrote: Question out of general interest in the language: If I would want to generate such functions in a for-loop, what would I have to do? This doesn't work: class Move(object): def

Re: Symbols as parameters?

2010-01-28 Thread Roald de Vries
On Jan 22, 2010, at 11:56 AM, Roald de Vries wrote: Hi Martin, On Jan 21, 2010, at 8:43 AM, Martin Drautzburg wrote: Hello all, When passing parameters to a function, you sometimes need a paramter which can only assume certain values, e.g. def move (direction): ... If

Re: Symbols as parameters?

2010-01-28 Thread Steven D'Aprano
On Thu, 28 Jan 2010 17:01:38 +0100, Roald de Vries wrote: Question out of general interest in the language: If I would want to generate such functions in a for-loop, what would I have to do? This doesn't work: class Move(object): def __call__(self, direction): return

Re: Symbols as parameters?

2010-01-24 Thread Gabriel Genellina
En Fri, 22 Jan 2010 10:16:50 -0300, Alf P. Steinbach al...@start.no escribió: I get the impression that there's some message traffic that I don't see, perhaps on the mailing list, since (a) I haven't seen that about 'locals' pointed out by anyone else in this thread, and I think I've

Re: Symbols as parameters?

2010-01-24 Thread Alf P. Steinbach
* Gabriel Genellina: En Fri, 22 Jan 2010 10:16:50 -0300, Alf P. Steinbach al...@start.no escribió: I get the impression that there's some message traffic that I don't see, perhaps on the mailing list, since (a) I haven't seen that about 'locals' pointed out by anyone else in this thread, and

Re: Symbols as parameters?

2010-01-24 Thread George Sakkis
On Jan 22, 8:39 pm, Martin Drautzburg martin.drautzb...@web.de wrote: Martin Drautzburg wrote: with scope():     # ...     # use up, down, left, right here # up, down, left, right no longer defined after the with block exits. Just looked it up again. It's a cool thing. Too bad my

Re: Symbols as parameters?

2010-01-24 Thread Alf P. Steinbach
Just top-posting for clarity. :-) code file=directions.py up = UP left= LEFT down= DOWN right = RIGHT /code code file=locals.py # This code is not guaranteed to work by the language specification. # But it is one way to do the solution I presented earlier in the thread. import

Re: Symbols as parameters?

2010-01-24 Thread Steven D'Aprano
On Sun, 24 Jan 2010 10:11:07 -0800, George Sakkis wrote: Both in your example and by using a context manager, you can get away with not passing locals() explicitly by introspecting the stack frame. You say that as if it were less of an ugly hack than the locals() trick. But sys._getframe is a

Re: Symbols as parameters?

2010-01-24 Thread George Sakkis
On Jan 25, 1:05 am, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Sun, 24 Jan 2010 10:11:07 -0800, George Sakkis wrote: Both in your example and by using a context manager, you can get away with not passing locals() explicitly by introspecting the stack frame. You say that

Re: Symbols as parameters?

2010-01-22 Thread Alf P. Steinbach
* Stefan Behnel: Alf P. Steinbach, 21.01.2010 20:24: Do you understand how bad that makes you look? I think the right thing to say at this point is don't feed the troll. I find it amazing that you continue this kind of ad hominem attack. You leave it open who you regard as trolling, but

Re: Symbols as parameters?

2010-01-22 Thread Martin Drautzburg
Steven D'Aprano wrote: I think this really is the correct solution for your problem. In Python, the standard place to have such public constants is at the module level, not the function or class. I think you're worrying unnecessarily about namespace pollution -- the module namespace is

Re: Symbols as parameters?

2010-01-22 Thread Martin Drautzburg
Carl Banks wrote: I see. Well, Python is a poor choice for defining an internal DSL (i.e., DSL using the general language's syntax), because it's (deliberately) rigid in both grammar and semantics. I had this impression too. Paul McGuire should be by to recommend PyParsing shortly. I

Re: Symbols as parameters?

2010-01-22 Thread Alf P. Steinbach
* Martin Drautzburg: Here is a complete expample using a decorator, still a bit noisy def move(aDirection): print moving + aDirection #Here comes the decorator def scope(aDict): def save(locals): Set symbols in locals and remember their original state setSymbols={}

Re: Symbols as parameters?

2010-01-22 Thread Carl Banks
On Jan 21, 11:43 pm, Martin Drautzburg martin.drautzb...@web.de wrote: Paul McGuire should be by to recommend PyParsing shortly. I looked it up and it seems to be about parsing strings. This is not what I am looking for as it would create a separate world outside of python. But I haven't

Re: Symbols as parameters?

2010-01-22 Thread Roald de Vries
Hi Martin, On Jan 21, 2010, at 8:43 AM, Martin Drautzburg wrote: Hello all, When passing parameters to a function, you sometimes need a paramter which can only assume certain values, e.g. def move (direction): ... If direction can only be up, down, left or right, you can

Re: Symbols as parameters?

2010-01-22 Thread Jean-Michel Pichavant
Steven D'Aprano wrote: (3) Then somone suggested to tie the constants to the function itself, as in def move(direction): print moving %s % direction move.UP = 'up' move.DOWN = 'down' This is quite nice. I would call it a horrible, horrible, horrible code smell. A stench in fact.

Re: Symbols as parameters?

2010-01-22 Thread Jan Kaliszewski
21-01-2010, 22:51:34 Martin Drautzburg martin.drautzb...@web.de wrote: Thanks for all the answers. Let me summarize (1) [...] (2) Using enum's was suggested. That is good to know, but again it is just a way to define constants in the caller's namespace. [...] (3) Then somone suggested to tie

Re: Symbols as parameters?

2010-01-22 Thread Jan Kaliszewski
s/sollution/solution s/event implemented/even implemented Sorry *j -- http://mail.python.org/mailman/listinfo/python-list

Re: Symbols as parameters?

2010-01-22 Thread Ben Finney
Jean-Michel Pichavant jeanmic...@sequans.com writes: Steven D'Aprano wrote: I would call it a horrible, horrible, horrible code smell. A stench in fact. […] As soon as it is properly documented, as a public interface should be, it becomes an acceptable design, IMO. […] So your position

Re: Symbols as parameters?

2010-01-22 Thread Dave Angel
Roald de Vries wrote: div class=moz-text-flowed style=font-family: -moz-fixedHi Martin, On Jan 21, 2010, at 8:43 AM, Martin Drautzburg wrote: Hello all, When passing parameters to a function, you sometimes need a paramter which can only assume certain values, e.g. def move

Re: Symbols as parameters?

2010-01-22 Thread Martin Drautzburg
On 22 Jan., 11:56, Roald de Vries r...@roalddevries.nl wrote: Hi Martin, On Jan 21, 2010, at 8:43 AM, Martin Drautzburg wrote: Hello all, When passing parameters to a function, you sometimes need a paramter which can only assume certain values, e.g.        def move (direction):

Re: Symbols as parameters?

2010-01-22 Thread Roald de Vries
On Jan 22, 2010, at 1:06 PM, Martin Drautzburg wrote: On 22 Jan., 11:56, Roald de Vries r...@roalddevries.nl wrote: Hi Martin, On Jan 21, 2010, at 8:43 AM, Martin Drautzburg wrote: Hello all, When passing parameters to a function, you sometimes need a paramter which can only assume

Re: Symbols as parameters?

2010-01-22 Thread Mark Dickinson
On Jan 21, 10:57 pm, Martin Drautzburg martin.drautzb...@web.de wrote: Here is a complete expample using a decorator, still a bit noisy def move(aDirection):     print moving + aDirection #Here comes the decorator def scope(aDict):     def save(locals): [...] Have you considered making

Re: Symbols as parameters?

2010-01-22 Thread Steven D'Aprano
On Fri, 22 Jan 2010 09:12:46 +0100, Martin Drautzburg wrote: Defining those symbols at the module level is absolutely fine with me. The namespace pollution is indeed my biggest worry. You see, I want to be able to type in lots of lines with little effort. Preferably I would want to type

Re: Symbols as parameters?

2010-01-22 Thread Jean-Michel Pichavant
Ben Finney wrote: Jean-Michel Pichavant jeanmic...@sequans.com writes: Steven D'Aprano wrote: I would call it a horrible, horrible, horrible code smell. A stench in fact. […] As soon as it is properly documented, as a public interface should be, it becomes an acceptable

Re: Symbols as parameters?

2010-01-22 Thread Steven D'Aprano
On Fri, 22 Jan 2010 09:29:18 +0100, Alf P. Steinbach wrote: But have you tested this within a function or class, which is what the use of locals implies? The reason that I ask is that in the documentation of locals() it says this: Note The contents of this dictionary should not be

Re: Symbols as parameters?

2010-01-22 Thread Alf P. Steinbach
* Steven D'Aprano - Alf P. Steinbach: No, you got it spot on. Not to discourage you, but you're at least the third person who pointed this out in this thread. I get the impression that there's some message traffic that I don't see, perhaps on the mailing list, since (a) I haven't seen that

Re: Symbols as parameters?

2010-01-22 Thread Wolfgang Rohdewald
On Friday 22 January 2010, Alf P. Steinbach wrote: I get the impression that there's some message traffic that I don't see For example, the recent thread Covert number into string started with a reply in my newreader, using EternalSeptember's NNTP host. It also starts with a reply in

Re: Symbols as parameters?

2010-01-22 Thread Alf P. Steinbach
* Steven D'Aprano: One implementation-specific trick is that modifying locals does actually work inside a class definition (at least in Python 2.5): class Foo(object): ... x = 1 ... print locals() ... locals()['x'] = 2 ... {'x': 1, '__module__': '__main__'} Foo.x 2 But it

Re: Symbols as parameters?

2010-01-22 Thread Alf P. Steinbach
* Wolfgang Rohdewald: On Friday 22 January 2010, Alf P. Steinbach wrote: I get the impression that there's some message traffic that I don't see For example, the recent thread Covert number into string started with a reply in my newreader, using EternalSeptember's NNTP host. It also starts

Re: Symbols as parameters?

2010-01-22 Thread Martin Drautzburg
Mark Dickinson wrote: On Jan 21, 10:57 pm, Martin Drautzburg martin.drautzb...@web.de wrote: Here is a complete expample using a decorator, still a bit noisy def move(aDirection): print moving + aDirection #Here comes the decorator def scope(aDict): def save(locals): [...] Have you

Re: Symbols as parameters?

2010-01-22 Thread Martin Drautzburg
Martin Drautzburg wrote: with scope(): # ... # use up, down, left, right here # up, down, left, right no longer defined after the with block exits. Just looked it up again. It's a cool thing. Too bad my locals() hack would still be required. The result would be less noisy (and

Re: Symbols as parameters?

2010-01-21 Thread Andre Engels
On Thu, Jan 21, 2010 at 8:43 AM, Martin Drautzburg martin.drautzb...@web.de wrote: Hello all, When passing parameters to a function, you sometimes need a paramter which can only assume certain values, e.g.        def move (direction):                ... If direction can only be up, down,

Re: Symbols as parameters?

2010-01-21 Thread Alf P. Steinbach
* Martin Drautzburg: Hello all, When passing parameters to a function, you sometimes need a paramter which can only assume certain values, e.g. def move (direction): ... If direction can only be up, down, left or right, you can solve this by passing strings, but this is

Re: Symbols as parameters?

2010-01-21 Thread Javier Collado
Hello, I'd say that isn't totally incorrect to use strings instead of symbols. Please note that in other programming languages symbols, atoms and the like are in fact immutable strings, which is what python provides by default. Best regards, Javier 2010/1/21 Alf P. Steinbach al...@start.no:

Re: Symbols as parameters?

2010-01-21 Thread Ben Finney
Martin Drautzburg martin.drautzb...@web.de writes: When passing parameters to a function, you sometimes need a paramter which can only assume certain values You might like to try the ‘enum’ library for this URL:http://pypi.python.org/pypi/enum. -- \ “You could augment an earwig to the

Re: Symbols as parameters?

2010-01-21 Thread Bearophile
Martin Drautzburg, having symbols spread in the namespace is bad. And too much magic is even worse. You seem to need something like an enum that must be used with its qualified name, as: move(direction.up) That works well unless the symbols name are keywords. Creating a small class like this

Re: Symbols as parameters?

2010-01-21 Thread Iain King
On Jan 21, 7:43 am, Martin Drautzburg martin.drautzb...@web.de wrote: Hello all, When passing parameters to a function, you sometimes need a paramter which can only assume certain values, e.g.         def move (direction):                 ... If direction can only be up, down, left or

Re: Symbols as parameters?

2010-01-21 Thread Alf P. Steinbach
Huh? I guess you meant to reply to the OP, not me. Cheers, - Alf * Javier Collado: Hello, I'd say that isn't totally incorrect to use strings instead of symbols. Please note that in other programming languages symbols, atoms and the like are in fact immutable strings, which is what python

Re: Symbols as parameters?

2010-01-21 Thread Carl Banks
On Jan 20, 11:43 pm, Martin Drautzburg martin.drautzb...@web.de wrote: Hello all, When passing parameters to a function, you sometimes need a paramter which can only assume certain values, e.g.         def move (direction):                 ... If direction can only be up, down, left or

Re: Symbols as parameters?

2010-01-21 Thread Alf P. Steinbach
* Carl Banks: On Jan 20, 11:43 pm, Martin Drautzburg martin.drautzb...@web.de [snip] What I am really looking for is a way - to be able to call move(up) - having the up symbol only in the context of the function call Short answer is, you can't do it. On the contrary,

Re: Symbols as parameters?

2010-01-21 Thread Jean-Michel Pichavant
Martin Drautzburg wrote: Hello all, When passing parameters to a function, you sometimes need a paramter which can only assume certain values, e.g. def move (direction): ... If direction can only be up, down, left or right, you can solve this by passing strings, but

Re: Symbols as parameters?

2010-01-21 Thread Stefan Behnel
Alf P. Steinbach, 21.01.2010 09:30: * Martin Drautzburg: - to be able to call move(up) - having the up symbol only in the context of the function call So it should look something like this ... magic, magic ... move(up) ... unmagic, unmagic ... print up Looks like a

Re: Symbols as parameters?

2010-01-21 Thread Stefan Behnel
Alf P. Steinbach, 21.01.2010 11:38: * Carl Banks: On Jan 20, 11:43 pm, Martin Drautzburg martin.drautzb...@web.de [snip] What I am really looking for is a way - to be able to call move(up) - having the up symbol only in the context of the function call Short answer is,

Re: Symbols as parameters?

2010-01-21 Thread Alf P. Steinbach
* Stefan Behnel: Alf P. Steinbach, 21.01.2010 09:30: * Martin Drautzburg: - to be able to call move(up) - having the up symbol only in the context of the function call So it should look something like this ... magic, magic ... move(up) ... unmagic, unmagic ... print up

Re: Symbols as parameters?

2010-01-21 Thread Alf P. Steinbach
* Stefan Behnel: Alf P. Steinbach, 21.01.2010 11:38: * Carl Banks: On Jan 20, 11:43 pm, Martin Drautzburg martin.drautzb...@web.de [snip] What I am really looking for is a way - to be able to call move(up) - having the up symbol only in the context of the function call

Re: Symbols as parameters?

2010-01-21 Thread Dave Angel
Martin Drautzburg wrote: Hello all, When passing parameters to a function, you sometimes need a paramter which can only assume certain values, e.g. def move (direction): ... If direction can only be up, down, left or right, you can solve this by passing strings, but

Re: Symbols as parameters?

2010-01-21 Thread Carl Banks
On Jan 21, 2:38 am, Alf P. Steinbach al...@start.no wrote: * Carl Banks: On Jan 20, 11:43 pm, Martin Drautzburg martin.drautzb...@web.de [snip] What I am really looking for is a way         - to be able to call move(up)         - having the up symbol only in the context of the

Re: Symbols as parameters?

2010-01-21 Thread Diez B. Roggisch
Am 21.01.10 12:58, schrieb Alf P. Steinbach: * Stefan Behnel: Alf P. Steinbach, 21.01.2010 11:38: * Carl Banks: On Jan 20, 11:43 pm, Martin Drautzburg martin.drautzb...@web.de [snip] What I am really looking for is a way - to be able to call move(up) - having the up symbol only in the

Re: Symbols as parameters?

2010-01-21 Thread Alf P. Steinbach
* Carl Banks: On Jan 21, 2:38 am, Alf P. Steinbach al...@start.no wrote: * Carl Banks: On Jan 20, 11:43 pm, Martin Drautzburg martin.drautzb...@web.de [snip] What I am really looking for is a way - to be able to call move(up) - having the up symbol only in the context of

Re: Symbols as parameters?

2010-01-21 Thread Alf P. Steinbach
* Diez B. Roggisch: Am 21.01.10 12:58, schrieb Alf P. Steinbach: * Stefan Behnel: Alf P. Steinbach, 21.01.2010 11:38: * Carl Banks: On Jan 20, 11:43 pm, Martin Drautzburg martin.drautzb...@web.de [snip] What I am really looking for is a way - to be able to call move(up) - having the up

Re: Symbols as parameters?

2010-01-21 Thread Diez B. Roggisch
Am 21.01.10 19:48, schrieb Alf P. Steinbach: * Diez B. Roggisch: Am 21.01.10 12:58, schrieb Alf P. Steinbach: * Stefan Behnel: Alf P. Steinbach, 21.01.2010 11:38: * Carl Banks: On Jan 20, 11:43 pm, Martin Drautzburg martin.drautzb...@web.de [snip] What I am really looking for is a way -

Re: Symbols as parameters?

2010-01-21 Thread Alf P. Steinbach
* Diez B. Roggisch: Am 21.01.10 19:48, schrieb Alf P. Steinbach: * Diez B. Roggisch: Am 21.01.10 12:58, schrieb Alf P. Steinbach: * Stefan Behnel: Alf P. Steinbach, 21.01.2010 11:38: * Carl Banks: On Jan 20, 11:43 pm, Martin Drautzburg martin.drautzb...@web.de [snip] What I am really

Re: Symbols as parameters?

2010-01-21 Thread Diez B. Roggisch
Am 21.01.10 20:01, schrieb Alf P. Steinbach: * Diez B. Roggisch: Am 21.01.10 19:48, schrieb Alf P. Steinbach: * Diez B. Roggisch: Am 21.01.10 12:58, schrieb Alf P. Steinbach: * Stefan Behnel: Alf P. Steinbach, 21.01.2010 11:38: * Carl Banks: On Jan 20, 11:43 pm, Martin Drautzburg

Re: Symbols as parameters?

2010-01-21 Thread Alf P. Steinbach
* Diez B. Roggisch: Am 21.01.10 20:01, schrieb Alf P. Steinbach: * Diez B. Roggisch: Am 21.01.10 19:48, schrieb Alf P. Steinbach: * Diez B. Roggisch: Am 21.01.10 12:58, schrieb Alf P. Steinbach: * Stefan Behnel: Alf P. Steinbach, 21.01.2010 11:38: * Carl Banks: On Jan 20, 11:43 pm,

Re: Symbols as parameters?

2010-01-21 Thread Carl Banks
On Jan 21, 10:46 am, Alf P. Steinbach al...@start.no wrote: * Carl Banks: On Jan 21, 2:38 am, Alf P. Steinbach al...@start.no wrote: * Carl Banks: On Jan 20, 11:43 pm, Martin Drautzburg martin.drautzb...@web.de [snip] What I am really looking for is a way         - to be able

Re: Symbols as parameters?

2010-01-21 Thread Alf P. Steinbach
* Carl Banks: On Jan 21, 10:46 am, Alf P. Steinbach al...@start.no wrote: * Carl Banks: On Jan 21, 2:38 am, Alf P. Steinbach al...@start.no wrote: * Carl Banks: On Jan 20, 11:43 pm, Martin Drautzburg martin.drautzb...@web.de [snip] What I am really looking for is a way - to be able

Re: Symbols as parameters?

2010-01-21 Thread Martin Drautzburg
Thanks for all the answers. Let me summarize (1) I fail to see the relevance of   def move( direction ): ...   print( move + str( direction ) ) ...   move( up ) move up not only in the context of my question. And I don't see an abuse of the language either. Maybe this could pass as a Zen

Re: Symbols as parameters?

2010-01-21 Thread Alf P. Steinbach
* Martin Drautzburg: Thanks for all the answers. Let me summarize (1) I fail to see the relevance of def move( direction ): ... print( move + str( direction ) ) ... move( up ) move up not only in the context of my question. And I don't see an abuse of the language either. Maybe this

Re: Symbols as parameters?

2010-01-21 Thread Diez B. Roggisch
Am 21.01.10 22:51, schrieb Martin Drautzburg: Thanks for all the answers. Let me summarize (1) I fail to see the relevance of def move( direction ): ... print( move + str( direction ) ) ... move( up ) move up not only in the context of my question. And I don't see an abuse of the

Re: Symbols as parameters?

2010-01-21 Thread Carl Banks
On Jan 21, 1:51 pm, Martin Drautzburg martin.drautzb...@web.de wrote: Thanks for all the answers. Let me summarize [snip] (2) Using enum's was suggested. That is good to know, but again it is just a way to define constants in the caller's namespace. It'll at least corral the symbols you want.

Re: Symbols as parameters?

2010-01-21 Thread Ben Finney
Carl Banks pavlovevide...@gmail.com writes: On Jan 21, 1:51 pm, Martin Drautzburg martin.drautzb...@web.de wrote: (2) Using enum's was suggested. That is good to know, but again it is just a way to define constants in the caller's namespace. It'll at least corral the symbols you want. It

Re: Symbols as parameters?

2010-01-21 Thread Steven D'Aprano
On Thu, 21 Jan 2010 22:51:34 +0100, Martin Drautzburg wrote: Thanks for all the answers. Let me summarize (1) I fail to see the relevance of   def move( direction ): ...   print( move + str( direction ) ) ...   move( up ) move up I'm glad it's not just me then. not only in the

Re: Symbols as parameters?

2010-01-21 Thread MRAB
Steven D'Aprano wrote: [snip] An example from the standard library: the re module defines constants I, L, M, etc. representing flags that are passed to the re.compile. They are implemented as integers so they can easily be combined with , but another implementation might use symbols. You will

Re: Symbols as parameters?

2010-01-21 Thread Steven D'Aprano
On Fri, 22 Jan 2010 02:42:55 +, MRAB wrote: Steven D'Aprano wrote: [snip] An example from the standard library: the re module defines constants I, L, M, etc. representing flags that are passed to the re.compile. They are implemented as integers so they can easily be combined with , but

Re: Symbols as parameters?

2010-01-21 Thread Martin Drautzburg
Here is a complete expample using a decorator, still a bit noisy def move(aDirection): print moving + aDirection #Here comes the decorator def scope(aDict): def save(locals): Set symbols in locals and remember their original state setSymbols={} unsetSymbols=[]

Re: Symbols as parameters?

2010-01-21 Thread Stefan Behnel
Alf P. Steinbach, 21.01.2010 20:24: Do you understand how bad that makes you look? I think the right thing to say at this point is don't feed the troll. Stefan -- http://mail.python.org/mailman/listinfo/python-list

Symbols as parameters?

2010-01-20 Thread Martin Drautzburg
Hello all, When passing parameters to a function, you sometimes need a paramter which can only assume certain values, e.g. def move (direction): ... If direction can only be up, down, left or right, you can solve this by passing strings, but this is not quite to the