should "self" be changed?
Ned Batchelder
ned at nedbatchelder.com
Tue May 26 16:04:14 EDT 2015
On Tuesday, May 26, 2015 at 3:47:20 PM UTC-4, Marko Rauhamaa wrote:
> zipher <dreamingforward at gmail.com>:
>
> > Would it be prudent to rid the long-standing "argument" (pun
> > unintended) about self and the ulterior spellings of it, by changing
> > it into a symbol rather than a name?
> >
> > Something like:
> >
> > class MyClass(object):
> >
> > def __init__(@):
> > @.dummy = None
> >
> > OR, even better, getting *rid of it* in the parameter list, so it
> > stops confusing people about how many parameters a method needs, and
> > transform it into a true *operator*.
>
> Python's practice works. However, a small problem is presented by nested
> classes:
>
> class Connection:
> def __init__(self):
> class Idle:
> def signal_start(self):
> # how to refer to the outer self
> :
> :
>
> Solutions include:
>
> ...
>
> class Connection:
> def __init__(self):
> conn = self
> class Idle:
> def signal_start(self):
> conn.set_state(Ready)
> :
>
>
> I have used the latter method recently.
I would find it much clearer to not use a nested class
at all, and instead to pass the object into the constructor:
class Idle:
def __init__(self, conn):
self.conn = conn
def signal_start(self):
self.conn.set_state(Ready)
class Connection:
def __init__(self):
something(Idle(self))
--Ned.
More information about the Python-list
mailing list