newbie: self.member syntax seems /really/ annoying

Dave Hansen iddw at hotmail.com
Wed Sep 12 09:33:38 EDT 2007


On Sep 12, 5:21 am, Charles Fox <charles.... at gmail.com> wrote:
> I've just started playing around with Python, as a possible
> replacement for a mix of C++, Matlab and Lisp.  The language looks
> lovely and clean with one huge exception:  I do a lot of numerical
> modeling, so I deal with objects (like neurons) described
> mathematically in papers, by equations like
>     a_dot = -k(a-u)
> In other languages, this translates nicely into code, but as far as I
> can tell, Python needs the ugly:
>     self.a_dot = -self.k(self.a-self.u)
> For large equations this is going to make my code seriously unreadable
> to the point of needing to switch back to Matlab -- and it seems to go
> against everything else about python's simplicity and elegance.  Am I

It goes back to the Zen of Python (import this): Explicit is better
than implicit.  And it's a boon to future maintainers of your code

> missing something?  Is there something like a 'with' command that lets
> me set the scope, like
>
>     with self:
>       .a_dot = -.k(.a-.u)

The name "self" is just a convention.  You can give it any name you
wish.  Using "s" is common.

As others have mentioned, you can also provide local synonyms if the
dot syntax is too onerous.  But make sure you use the member access
syntax if the result of an expression is an immutable type.  For
example, I assume a_dot is a mutable type, such as a list.  If so,
then you could simply use

   a_dot, k, a, u = self.a_dot, self.k, self.a, self.u
   a_dot = -k(a-u)

However, if it's an immutable type, such as a scalar, strung, or
tuple, you need

   self.a_dot = -k(a-u)

And it gets trickier if you have members taking intermediate values...

>
> It's premature to make language suggestions as I am new to the

That's for sure  ;-)

Regards,

   -=Dave




More information about the Python-list mailing list