Symbols as parameters?
Roald de Vries
rdv at roalddevries.nl
Fri Jan 22 07:29:22 EST 2010
On Jan 22, 2010, at 1:06 PM, Martin Drautzburg wrote:
> On 22 Jan., 11:56, Roald de Vries <r... at 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):
>>> ...
>>> If direction can only be "up", "down", "left" or "right", you can
>>> solve
>>> this by passing strings, but this is not quite to the point:
>>
>>> - you could pass invalid strings easily
>>> - you need to quote thigs, which is a nuisance
>>> - the parameter IS REALLY NOT A STRING, but a direction
>>
>>> Alternatively you could export such symbols, so when you "import *"
>>> you
>>> have them available in the caller's namespace. But that forces you
>>> to "import *" which pollutes your namespace.
>>
>>> 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
>>
>>> So it should look something like this
>>
>>> ... magic, magic ...
>>> move(up)
>>> ... unmagic, unmagic ...
>>> print up
>>
>>> This should complain that "up" is not defined during the "print"
>>> call,
>>> but not when move() is called. And of course there should be as
>>> little
>>> magic as possible.
>>
>>> Any way to achieve this?
>>
>> You could do something like this:
>>
>> class Move(object):
>> def __call__(self, direction):
>> print(direction)
>> return 0
>>
>> def up(self):
>> return self('up')
>>
>> move = Move()
>>
>> Now move.up() means move('up'), and you can obviously do similar
>> things for other directions.- Zitierten Text ausblenden -
>>
>> - Zitierten Text anzeigen -
>
> I had thought about that too. It gets a bit tricky when there is more
> than one parameter and it completely fails whan a parameter can REALLY
> be a number or an arbitrary string. Think: move(direction, distance)
For extra parameters:
class Move(object):
def __call__(self, direction, param1, param2, *params, **dict):
print(direction)
return 0
def up(self, *params, **dict):
return self('up', *params, **dict)
move = Move()
On Jan 22, 2010, at 1:06 PM, Martin Drautzburg wrote:
> On 22 Jan., 11:56, Roald de Vries <r... at 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):
>>> ...
>>> If direction can only be "up", "down", "left" or "right", you can
>>> solve
>>> this by passing strings, but this is not quite to the point:
>>
>>> - you could pass invalid strings easily
>>> - you need to quote thigs, which is a nuisance
>>> - the parameter IS REALLY NOT A STRING, but a direction
>>
>>> Alternatively you could export such symbols, so when you "import *"
>>> you
>>> have them available in the caller's namespace. But that forces you
>>> to "import *" which pollutes your namespace.
>>
>>> 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
>>
>>> So it should look something like this
>>
>>> ... magic, magic ...
>>> move(up)
>>> ... unmagic, unmagic ...
>>> print up
>>
>>> This should complain that "up" is not defined during the "print"
>>> call,
>>> but not when move() is called. And of course there should be as
>>> little
>>> magic as possible.
>>
>>> Any way to achieve this?
>>
>> You could do something like this:
>>
>> class Move(object):
>> def __call__(self, direction):
>> print(direction)
>> return 0
>>
>> def up(self):
>> return self('up')
>>
>> move = Move()
>>
>> Now move.up() means move('up'), and you can obviously do similar
>> things for other directions.- Zitierten Text ausblenden -
>>
>> - Zitierten Text anzeigen -
>
> I had thought about that too. It gets a bit tricky when there is more
> than one parameter
For extra parameters:
class Move(object):
def __call__(self, direction, param1, param2, *params, **dict):
print(direction)
return 0
def up(self, *params, **dict):
return self('up', *params, **dict)
move = Move()
> and it completely fails whan a parameter can REALLY
> be a number or an arbitrary string. Think: move(direction, distance)
For a number, move.up(10) wouldn't be too bad. For an arbitrary
string, you always have __getattr__ and __getattribute__. But if you
like this style of coding, you should probably switch to Ruby.
More information about the Python-list
mailing list