Is it possible to create a shortcut ?

S

Stef Mientki

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

something like this:
self.P[0] =


is that possible, otherwise than by eval / exec ?

thanks,
Stef
 
P

Peter Otten

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

something like this:
self.P[0] =


is that possible, otherwise than by eval / exec ?

class Brick(object):
def __init__(self):
self.Par = [1,2,3]

class P(object):
def __init__(self, parent):
self.parent = parent
def __setitem__(self, index, value):
p = self.parent
p.Brick.Par[p.EP[index]] = value

class A(object):
def __init__(self):
self.Brick = Brick()
self.EP = [2,1,0]
self.P = P(self)

def demo(self):
print "before:", self.Brick.Par
self.P[0] = "shortcut"
print "after:", self.Brick.Par

a = A()
a.demo()

You can of course simplify things if you move the P.__setitem__() method
into A and drop the helper class P.

Peter
 
S

Steven D'Aprano

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.
 
S

Stef Mientki

Steven said:
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'.
I fully agree, that's exactly why I want a simpler form ;-)

This is part of a virtual machine so it need to be fast,
and with the fewest possible interface names.

Yes, you might wonder why I can't solve such a simple (after knowing the
answer) question myself,
while I'm designing a kind of virtual machine.
And when I'm working on the top level of the virtual machine,
I forget all the tiny details of Python itself,
which is btw the goal of the virtual machine ;-)

I took some Peters code, and it works like a charm.

thanks guys.

cheers,
Stef Mientki
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,202
Latest member
ClaudioVil

Latest Threads

Top