Is it possible to create a shortcut ?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Mar 21 13:27:41 CET 2009


On Sat, 21 Mar 2009 11:55:04 +0100, Stef Mientki wrote:

> I would like to make a shortcut for this:
>     self.Brick.Par [ self.EP[0] ] =

That's a pretty ugly expression there. (Mind you, I've seen worse.) And a 
non-standard naming convention. I'm just sayin'.

When you walk your dog, do you try to tell it how to move its legs?

http://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/
LawOfDemeter.htm

The Law of Demeter suggests that self shouldn't manipulate the brick's 
internal components. To do so is rather like telling your dog how to move 
its legs. Better to give Brick an appropriate method, and then call that.


> something like this:
>    self.P[0] =
> 
> 
> is that possible, otherwise than by eval / exec ?

But if you absolutely insist... 

# Untested
class Whatever(object):
    # ... more definitions here ...
    def P(self, index, value):
        self.Brick.Par [ self.EP[index] ] = value


Alternatively, some variation of Peter Otten's code should do what you 
like, and completely confuse your dog.



-- 
Steven



More information about the Python-list mailing list