Hygenic Macros

Robert Kern robert.kern at gmail.com
Tue Oct 18 16:42:21 EDT 2005


Steven D'Aprano wrote:
> On Mon, 17 Oct 2005 22:23:43 -0700, David Pokorny wrote:
> 
>>Hi,
>>
>>Just wondering if anyone has considered macros for Python. I have one 
>>good use case. In "R", the statistical programming language, you can 
>>multiply matrices with A %*% B (A*B corresponds to pointwise 
>>multiplication). In Python, I have to type
>>
>>import Numeric
>>matrixmultiply(A,B)
>>
>>which makes my code almost unreadable.
> 
> Yes, I see what you mean, it is pretty confusing. It almost looks like
> a function that multiplies two matrices and returns the result. 
> 
> Have you tried coming up with better names for your arguments than A and
> B? Many people find that using self-documenting variable names helps make
> code easier to understand.

Well, to be fair, his example was trivial. When you have more
complicated matrix expressions with transposes and conjugations and more
matrixmultiplies than you can shake a stick at, it gets ugly pretty fast.

  F = dot(dot(Z, F),transpose(conjugate(Z)))

versus

  from scipy import *
  F = mat(F)
  Z = mat(Z)
  F = Z*F*Z.H

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter




More information about the Python-list mailing list