Justin Francis Self-Portrait

Saturday, May 5, 2007

Love Your self In Python

When I was using Java, I always insisted on making the this reference explicit when referring to an attribute or method within the same class. I think it always makes the code clearer because there is never any doubt about where an identifier is coming from; either it is a temp or argument or there will be a this in front of it, in which case it is an attribute or method.

The habit of placing this in Java code even where it was not required certainly eased my transition to Python. In Python, self is the explicit first parameter to every method, and must be used whenever accessing methods or attributes of the object. I have often heard people complain about this, but it is one of the things I love about Python.

In addition to the clarity described above, the explicit self as the first parameter to all methods unifies functions and methods in a way that is not done in other languages. Once you start thinking about methods as functions that take an object as their first argument, functions and methods become very similar. This encourages a dual interface that is used often in Python. This dual interface is one where you can use either a function or an object's method to do the same thing (like Python's re module).

For example, I find myself often writing code like this:

def get_table_sql(name, db):
return 'CREATE TABLE %s (%s)' % (name, get_column_sql(name))

class Table(DbObject):
def get_sql(self):
return get_table_sql(self.name, self.db)

Now, nothing prevents you from doing this in other languages, but I find that the explicit self goes a long way towards encouraging programmers to think about functions and methods in similar ways. And at the end of the day, your design is based more upon your frame of mind than the technological limitations of the language you are using.


Unknown said...

coach outlet
tory burch outlet online
michael kors outlet
timberland canada
coach factory outlet
burberry handbags
pandora uk
true religion outlet
gucci outlet
coach outlet store online
louis vuitton outlet
michael kors outlet
ralph lauren uk
jordan shoes
louis vuitton
jeremy scott adidas
chanel handbags
cheap oakley sunglasses
jordan 11s
hollister clothing
copy watches
prada bags
p90x workouts
polo outlet
jordan retro 3
coach outlet
pandora jewelry
hollister co
retro jordan
pandora rings
coach outlet online
jordan retro 8
ray ban sunglass
air jordans
oakley frogskins

Anonymous said...

michael kors outlet
ralph lauren outlet
mont blanc pens
asics,asics israel,asics shoes,asics running shoes,asics israel,asics gel,asics running,asics gel nimbus,asics gel kayano
mulberry uk
swarovski jewelry
ralph lauren uk
longchamp pliage
pandora jewelry
thomas sabo uk
hollister shirts
adidas trainers
coach outlet
michael kors wallet
fitflops sale
true religion jeans
louis vuitton bags
adidas shoes
hermes bags
kate spade uk
toms outlet
louis vuitton outlet stores
louis vuitton neverfull sale
discount michael kors handbags
longchamp handbags
prada outlet
michael kors factory outlet
louis vuitton handbags
michael kors handbags
polo ralph lauren
true religion jeans
cartier outlet store
michael kors outlet
omega outlet

chenmeinv0 said...

michael kors outlet clearance
burberry outlet
louis vuitton purses
rolex watches outlet
louboutin pas cher
mbt shoes
louboutin shoes
coach outlet online
canada goose sale
cheap uggs
michael kors outlet
montblanc pens
cheap ugg boots
louis vuitton outlet
chanel outlet
fitflop sandals
coach outlet online
michael kors
coach outlet
adidas trainers uk
ugg outlet
jordan shoes
cavaliers jerseys
tiffany and co jewelry
cheap rolex watches
doudoune canada goose
chaussures ugg
ray ban sunglasses outlet
cheap jordan shoes
louis vuitton handbags

Unknown said...

polo ralph lauren outlet
michael kors outlet
goose outlet
snapbacks wholesale
abercrombie and fitch
uggs australia
louis vuitton
louis vuitton canada
longchamp outlet