Macro like functionality for shorthand variable names

  • Thread starter Tilman Kispersky
  • Start date
T

Tilman Kispersky

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

Kay Schluehr

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
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
 
P

Paul Hankin

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)
 

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
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top