newbie: self.member syntax seems /really/ annoying

genro giovanni.porcari at gmail.com
Thu Sep 13 02:48:15 EDT 2007


On Sep 12, 6: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
> missing something?  Is there something like a 'with' command that lets
> me set the scope, like
>
>     with self:
>       .a_dot = -.k(.a-.u)
>
> It's premature to make language suggestions as I am new to the
> language, but I would have though that making a 'with self' explicit
> in all methods would have been neat, so I could just write
>       .a_dot = -.k(.a-.u)
> which would still avoid confusion with local function variables, since
> '.a' is different from 'a'.
>
> Please help if I am missing something -- this looks like a great
> language but I am going to mad trying to read numerical code full of
> 'self.'s breaking up the equations.

Just an idea: I think that if you have to get a more readable code
you
could abstract the function from the object using a significative
name and then use this named function passing **self.__dict__

For example in your module you could write:

#------ functional section -----------


def dothis(k,a,u):
    return k*(a-u)

def dothat(m,r,k):
    return m*r+(k.m)^m


#----- Class definition ------

class Bar(object):

    def foo(self):
        self.a_dot = dothis(**self.__dict__)
        self.boo = dothat(**self.__dict__)

In this way the functional part is pure expression and very readable.


HTH

G.




More information about the Python-list mailing list