the annoying, verbose self

samwyse samwyse at gmail.com
Sun Nov 25 14:28:20 CET 2007


On Nov 24, 1:10 pm, "Patrick Mullen" <saluk64... at gmail.com> wrote:
> If there were a "using" or if the with statement would handle
> something like this, I wouldn't use it.  "s." is only 2 characters.  I
> saw chained dots mentioned.  Chained dots are 2 characters.  Why are
> we still discussing this?  "s." is the answer, or pulling the
> attributes into local vars if you are going to use them many times, to
> save lookup.  This is not a band-aid, this is an actual valid
> programming technique.  There is more to programming than typing...

Actually, the chained dots are solving a completely different problem,
that of refactoring a collection of functions that use global vars
into a class.

Although I'm now wondering if I could jigger something together using
globals() to make a top-level self-like object.  Hmmm, maybe someting
like this:

>>> class GlobalSelf(object):
        def __init__(self):
            self.__dict__ = globals()

>>> x = 42
>>> def myfunc(*args):
        "something that I may want to refactor later"
        s = GlobalSelf()
        s.x += 1

>>> x
42
>>> myfunc()
>>> x
43



More information about the Python-list mailing list