Skip to main content

Automagically add 'self' argument to method definitions

Project description

autoself: automagically add 'self' argument to method definitions.

First, a disclaimer. Explicit self is good. Bytecode hacks are bad.
Put them together and it's quite clear that THIS MODULE IS AN ABOMINATION!
But, it's a neat excursion into python's lower levels and if you *really*
*really* want to save yourself some keystrokes (like, you're desperately
trying to hack into the Death Star's security system to override the trash
compactor as its cold metal jaws slowly squeeze you to a purple paste) then
it can help you do that. But, stop and consider Guido's proclamation on
the matter:

Having self be explicit is a *good thing*. It makes the code clear by
removing ambiguity about how a variable resolves. It also makes the
difference between functions and methods small.
"Things that will Not Change in Python 3000":
http://www.python.org/dev/peps/pep-3099/#core-language

This module is not about making 'self' implicit. It doesn't try to change
the way methods work, or make any semantic changes whatsoever. It does one
simple thing: automatically adds the 'self' argument to method definitions.
Think of it as a post-processor for method definitions.

It provides a single function <autoself>. Given a function as argument,
<autoself> will return an equivalent function that takes the variable 'self'
as an extra argument in position zero. If the function does not refer to a
variable named 'self', then it is returned unmodified.

For example, defining the method <likes> using:

def likes(self,ham,eggs):
print self, "likes", ham, "and", eggs

Is equivalent to defining it in the following way:

def likes(ham,eggs):
print self, "likes", ham, "and", eggs
likes = autoself(likes)

Or neater, using the @autoself decorator. Of course, this isn't going to save
you any typing! <autoself> can also be applied to a class, and will
autoselfify all functions in that class's dict:

class HeapsLessTyping:
def likes(ham,eggs):
print self, "likes", ham, "and", eggs
def hates(spam):
print self, "hates", spam
HeapsLessTyping = autoself(HeapsLessTyping)

When it becomes available (Python 2.6?), it will be even more convenient to
use this with the class decorator syntax:

@autoself
class HeapsLessTyping:
def likes(ham,eggs):
print self, "likes", ham, "and", eggs
def hates(spam):
print self, "hates", spam

Want to save even more typing? <autoself> can be used as a metaclass to
work it's magic on all classes defined in a module:

__metaclass__=autoself
class LookNoSelf:
def __init__(my,special,args):
self.my = my
self.special = special
self.args = args
class FiveKeystrokesSaved:
def __init__(this,works,great):
self.this = this
self.works = works
self.great = great

Using this style, you will see a net saving in keystrokes with only
five method definitions per module!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

autoself-1.0.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distributions

autoself-1.0.0-py2.5.egg (12.3 kB view details)

Uploaded Source

autoself-1.0.0-py2.4.egg (12.3 kB view details)

Uploaded Source

File details

Details for the file autoself-1.0.0.tar.gz.

File metadata

  • Download URL: autoself-1.0.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for autoself-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f2f2161305feb171962de19c43c1f19984ee14438d6f7ca28ea6b3df190490be
MD5 aaac64e0a59916d6ebfc96550cc9c945
BLAKE2b-256 4e10d00a07da4f8abcf6d200c56b0101d0f027d330a63907d816f900c156b65c

See more details on using hashes here.

File details

Details for the file autoself-1.0.0-py2.5.egg.

File metadata

  • Download URL: autoself-1.0.0-py2.5.egg
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for autoself-1.0.0-py2.5.egg
Algorithm Hash digest
SHA256 ad324c5fd4a47c3d45268ce443daddfdf71ec5c846f5c32f7a9106c04a015246
MD5 95af28c6249972edb5d6f3629ca52fc0
BLAKE2b-256 ce0443772ffd81ceb2286ad2801e90a4ea88a7f927d765a3f2118676bdd4a094

See more details on using hashes here.

File details

Details for the file autoself-1.0.0-py2.4.egg.

File metadata

  • Download URL: autoself-1.0.0-py2.4.egg
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for autoself-1.0.0-py2.4.egg
Algorithm Hash digest
SHA256 a90eb7bcbfc934eb927553f07f5125cf0961429e9a3eaecb245c1caae242223f
MD5 733fdd70cd08d44f34bfe45c9a42ac39
BLAKE2b-256 49c0a12aeb2a13323f720a63115d4c49bf4845713b0aeac9ca8b82ee5c2878bf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page