By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

Macro like functionality for shorthand variable names

P: n/a
I have python code in a class method translated from C++ that looks
sort of like this:
>> self.dydt[1] = self.a * (self.b * self.y[0] - self.y[1])

To make this more readable in C++ I had made macros to achieve this:
#define du (dydt[1])
#define u (y[1])
#define V (y[0])

du = a * (b * V - u);
I realize the value of not having macros in Python. They've tripped
me up more than once in C++. My question is:
Is there any way to write a shorterhand more readable version of the
python code above? I'm doing several calculations one after the other
and some of the lines are quite long.
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 6 Jun., 23:13, Tilman Kispersky <tilman...@gmail.comwrote:
I have python code in a class method translated from C++ that looks
sort of like this:
> self.dydt[1] = self.a * (self.b * self.y[0] - self.y[1])

To make this more readable in C++ I had made macros to achieve this:
#define du (dydt[1])
#define u (y[1])
#define V (y[0])

du = a * (b * V - u);

I realize the value of not having macros in Python. They've tripped
me up more than once in C++. My question is:
Is there any way to write a shorterhand more readable version of the
python code above? I'm doing several calculations one after the other
and some of the lines are quite long.
There is no entirely generic way but you can define a function that
produces a string from an object that contains assignments and then
use exec:

def tovars(obj):
return ";".join("%s=%s"%(n,v) for (n,v) in obj.__dict__.items())

# example

class A:pass
>>a = A()
a.x = 0
a.y = 1
exec tovars(a)
y
1
>>x
0

In your own case the tovars) function might be defined as:

def tovars(obj):
assign = []
assign.append("du = %s"%obj.dydt[1])
assign.append("u = %s"%obj.y[1])
assign.append("V = %s"%obj.y[0])
return ";".assign

Jun 27 '08 #2

P: n/a
Tilman Kispersky wrote:
I have python code in a class method translated from C++ that looks
sort of like this:
>>> self.dydt[1] = self.a * (self.b * self.y[0] - self.y[1])


To make this more readable in C++ I had made macros to achieve this:
#define du (dydt[1])
#define u (y[1])
#define V (y[0])

du = a * (b * V - u);
I realize the value of not having macros in Python. They've tripped
me up more than once in C++. My question is:
Is there any way to write a shorterhand more readable version of the
python code above? I'm doing several calculations one after the other
and some of the lines are quite long.
Names in Python point to objects so you can bind several names to the same location.
>>> self.dydt[1] = self.a * (self.b * self.y[0] - self.y[1])
The following will work.

a = self.a
b = self.b
u = self.y[0]
V = self.y[1]

self.dytd[1] = a * (b * V - u)

In addition to making the lines shorter, I'm pretty sure they will be slightly
faster because lookups on the local variables is slightly faster than on the
instance variables (self.?).

Note: I think your parenthesis are incorrect in the equation above. If they
aren't then no parenthesis are necessary since multiplication takes precedence
over subtraction.

Hope this helps.

-Larry
Jun 27 '08 #3

P: n/a
On Jun 7, 6:13*am, Tilman Kispersky <tilman...@gmail.comwrote:
I have python code in a class method translated from C++ that looks
sort of like this:
>*self.dydt[1] = self.a * (self.b * self.y[0] - self.y[1])

To make this more readable in C++ I had made macros to achieve this:
#define du (dydt[1])
#define u (y[1])
#define V (y[0])

du = a * (b * V - u);

I realize the value of not having macros in Python. *They've tripped
me up more than once in C++. *My question is:
Is there any way to write a shorterhand more readable version of the
python code above? *I'm doing several calculations one after the other
and some of the lines are quite long.
First, you can use 's' rather than 'self'.

Then, you can make 'du', 'u' and 'V' properties of your class like
this:
class MyClass(object):
... add this at the end of your class definition ...
def _getu(self): return self.y[1]
def _setu(self, u): self.y[1] = u
u = property(_getu, _setu)
... and similarly for du, V

Using these two tricks, your code line would be:
s.du = s.a * (s.b * s.V - s.u)

--
Paul Hankin
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.