Another reason assignment expressions would be nice (?)

Tim Peters tim_one at email.msn.com
Sat Jul 10 23:50:42 EDT 1999


[alan at littleford.net]
> ...
> Now here is the rub, I might want two lines generated with a related
> value. Lets assume SomeFunction() throws a random number, so I can't
> just call it again, so what I would like to do is this:
>
> 	a = foo()
> 	a.setTemplate("'A test line %d' %
> 		(self.i = self.SomeFunction()"))
> 	a.setTemplate("'Same value as above %d' % self.i")
> 	...
> 	a.run(100)
>
> But of course I can't do this because '=' is a statement in the first
> template.
>
> [and skipping insane alternatives]
>
> ...
> All in all it seems I have to jump through hoops because i = 3
> doesn't 'return' the value 3.
>
> Or am I missing something frightfully easy here ..

Writing code to generate code with magical side-effect instance attr binding
via magical side-effect embedded assignments ... oh ya, sign me up for two
of those <wink>.

How about

    a = foo()
    a.setAttr('i', 'self.SomeFunction()')
    a.setTemplate("'A test line %d' % self.i")
    a.setTemplate("'Same value as above %d' % self.i")

?  Then (a) the code is easier to write; and, (b) when it all falls apart
you can at least find your mutations in an editor via searching for
"setAttr", or even turn on a debug flag causing setAttr to log what it's
doing.

different-operations-different-methods-ly y'rs  - tim






More information about the Python-list mailing list