Ramblings

March 12, 2008

Getters and setters in Python

Filed under: dev, python — michaelangela @ 3:54 pm

This is Python 2.2+ but it’s a good example. This page has a lot of Python-think on it as well.

Python Getters and Setters

Properties: attributes managed by get/set methods Properties are a neat way to implement attributes whose usage resembles attribute access, but whose implementation uses method calls. These are sometimes known as “managed attributes”. In prior Python versions, you could only do this by overriding __getattr__ and __setattr__; but overriding __setattr__ slows down all attribute assignments considerably, and overriding __getattr__ is always a bit tricky to get right. Properties let you do this painlessly, without having to override __getattr__ or __setattr__.

Table of Contents

Python one liner

Filed under: dev, python — michaelangela @ 2:47 pm

This is a nice one liner, and it just happened to be there, totally unrelated to the article. Well it’s used but it’s not the point. But still it’s nice.

Metaclass Programming in Python, Part 2: — Understanding the Arcana of Inheritance and Instance Creation —

for s in “Power Wealth Beauty”.split(): exec ‘%s=”%s”‘%(s,s)

The result is:
In [15]: for s in “Power Wealth Beauty”.split(): exec ‘%s=”%s”‘%(s,s)
….:
In [16]: Power

Out[16]: ‘Power’
In [17]: Wealth

Out[17]: ‘Wealth’
In [18]: Beauty

Out[18]: ‘Beauty’

So it’s a quick way to take some text and assign it to a variable with a value of itself.

More metaclass programming in Python

Filed under: dev, python — michaelangela @ 2:05 pm

This is a bit more up to date and also a bit more detailed. It’s a two part article as well.

Metaclass Programming in Python: — Pushing Object Oriented Programming to the Next Level —

Metaclasses enable certain types of “aspect oriented programming,” e.g. allow you to enhance classes with features like tracing capabilities, object persistence, exception logging, and more.

The use of python metaclasses

Filed under: dev, python — michaelangela @ 1:55 pm

I have to say… it was a bit mind blowing but I do see the power. The example given, while dated, is helpful. This was apparently still early on in the development of the concept.

Metaclasses in Python 1.5

Now let’s see how we could use metaclasses — what can we do with metaclasses that we can’t easily do without them? Here’s one idea: a metaclass could automatically insert trace calls for all method calls. Let’s first develop a simplified example, without support for inheritance or other “advanced” Python features (we’ll add those later).

Van Rossum’s intro to Metaclasses

Filed under: dev, python — michaelangela @ 1:40 pm

Even though it includes this warning, I’ll take my chances… but there is a good chance it’ll make my brain explode… ๐Ÿ™‚

Metaclasses in Python 1.5

Warning: reading this document may cause your brain to explode.

A Gentle(r) Intro to Metaclass Programming

Filed under: dev, python — michaelangela @ 1:33 pm

Metaclass programming… new to me and needed as I am seeing it everywhere. I just recently found out that Python has “list comprehensions” and what those means. Sure it’s in the docs but I hadn’t gotten to that part… ๐Ÿ˜ฆ So metaclass programming…

Guido van Rossum, the creator of Python, wrote his own intro to metaclass programming for Python 1.5. But in it he says,

(Postscript: reading this essay is probably not the best way to
understand the metaclass hook described here. See a message posted by Vladimir Marangozov
which may give a gentler introduction to the matter. You may also
want to search Deja News for messages with “metaclass” in the subject
posted to comp.lang.python in July and August 1998.)

So that’s what I am reading now. It’s actually quite good.

python’s sys._getframe

Filed under: dev, python — michaelangela @ 2:00 am

Get the calling function

ASPN : Python Cookbook : Automatically initializing instance variables from __init__ parameters

callerName = sys._getframe(1).f_code.co_name

Create a free website or blog at WordPress.com.