Why self?
Alex Martelli
aleax at aleax.it
Thu Jul 11 03:21:47 EDT 2002
More information about the Python-list mailing list
Thu Jul 11 03:21:47 EDT 2002
- Previous message (by thread): Why self?
- Next message (by thread): Why self?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
David LeBlanc wrote:
> Louis;
>
> I think he was proposing an idea. I don't believe you can import from
> self,
You can, but of course you'll get stuff from a module such as self.py
or an object that's already in sys.modules['self']. The latter could
be an instance of a peculiar class, so it WOULD be possible to play
very dirty tricks -- a __getattr__ method that looks into the caller's
locals for a variable named 'self' and delegates the attribute requests
to it. Very, VERY dirty:-).
> and i'm almost positive that "to ... export" isn't part of Python.
Right, that syntax can't be made to work, but the semantics are easy.
Sometimes I tire of writing a gazillion boilerplatish
self.somename = somename
and take a shortcut, e.g. in the currently online version of YAPTU,
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52305 ,
there's a:
def __init__(self, regex=_never, adict={},
restat=_never, restend=_never, recont=_never,
preproc=identity, handle=nohandle, ouf=sys.stdout):
"Initialize self's attributes"
self.regex = regex
self.globals = adict
# etc, etc, ETC, ... ad nauseam ...
but for the printed version I changed __init__'s body into:
def self_set(**kwds): self.__dict__.update(kwds)
self_set(**vars())
a "bit" sloppier (sets self.self which in turn makes a loop... that,
at least, could be specialcased away in self_set!-) but half a page
shorter. Boilerplate is error-prone -- whenever you (e.g.) change
an argument-and-attribute's name, the boilerplate version forces you
to change it in at least three places... the kind of mindless boring
task that in the long range produces numerous silly little errors.
Alex
- Previous message (by thread): Why self?
- Next message (by thread): Why self?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list