Sunday, September 14, 2008

PyCon UK: Class Decorators, Radically Simple

I'm giving a new version of my EuroPython talk, Class Decorators: Radically Simple.


Two thirds of the slides are new. There is much more front end emphasis on what decorators are and simple function decorators examples. The examples of class decorators and metaclasses have also changed.

Post Con

I changed the slide that used the old 3rd party decorator module to use the standard lib functools equivalent.

Speakers thoughts: The details of this talk were substantially different than the EuroPython version. I had more slides but there was less talky-talk involved with each one so it ended up the same at about twenty minutes. I intend to rejigger it again and submit it to PyCon as a mix of the two talks. The more concrete slides at PyCon UK went over well but most of the metaclass banter fell off and all the esoteric stuff (which will probably stay dead). The topical slide about getting my passport was skipped during the presentation - I did it as a lightning talk instead.

I wasn't nervous for the talk itself, there were maybe 75 people in the audience. I was nervous for my 5 minute lightning talk immediately afterward "How to get a new US passport same-day while committing the minimum number of felonies." I don't know why - I've spoken in front of larger crowds before and the idea of lightning talks is that they aren't expected to be polished (I had no slides and just spoke extemporaneously). The lightning talk went well and might have tied for the highest number of laugh-lines with the other humorous talk "Why I love Zeppelins."

Next time I intend to cheat and take some Beta Blockers before any speech. For those who don't know: beta blockers repress the physical symptoms of the fight-or-flight reflex only and don't change your brain. So they will suppress a quiver in your voice or hands but don't help with "umms" or "aahs." The only way to suppress the um/ah reflex is to practice before hand - a mirror works fine; just have enough canned variations on the topic that you can pick and choose the most suitable one at runtime which is more fluid than you think.

I only had one question during the talk and one question afterward. I'll have to think of ways to introduce more uncertainly into the talk for my next version. The first version At EuroPython elicited two or three questions during the talk and two or three after. Based on that feedback I answered those in this version but I'd like to generate some new ones next time.

9 comments:

Anonymous said...

In your presentation you mention that decorators are once and metaclasses are always. What do you exactly mean by that. Nothing in PEP for class decorators or simple trial confirms my understanding that decorators don't affect derived classes, but they apparently do:

def extra_method(self):
print("Extra!")

def mdeco(fn):
def mdeco_meth(self,*argv,**kw):
print("Method decorated")
return fn(self,*argv,**kw)
return mdeco_meth

def cdeco(cls):
setattr(cls,'mymethod',extra_method)
return cls

@cdeco
class A:
@mdeco
def amethod(self):
print("amethod")

class B(A):
pass

a=A()
a.mymethod()
a.amethod()

b=B()
b.mymethod()
b.amethod()

....
Extra!
Method decorated
amethod
Extra!
Method decorated
amethod
....

(sorry about mangled code - couldn't figure out how to make blogger preserve formatting)

Jack Diederich said...

The decorators are only getting run when explicitly called. You are using the decos to change the class methods to print each time they are called.

For instance the "Method decorated" print statement should be the line before mdeco_meth or its text should be "replacement function called"

Anonymous said...

thanks! now I knew that before but for whatever reason decorator stuck with me as a function wrapped inside of decorator and not decorator itself. So in other words decorator code would be executed only once per *class definition* and metaclass code would be executed for every *object instatiation*.
For what it's worth, maybe it's not a bad idea to add something as explicit as your explanation into the presentation? (for people like me ;) )

Jack Diederich said...

The talk isn't the same as the slides. There is a minute or two of me talking for each slide in that deck. This isn't to make people come to the talk instead of just downloading the slides; it's just the way the format works.

So what you don't see on the slides is me saying repeatedly: the decorator is the function that takes one argument and returns something similar (class for a class, func for a func). That is important to remember because functions get nested and sometimes you end up with a decorator-making function that returns a decorator which then returns another function. The decorator is the one that takes one (function|class) as an argument and returns a similar (function|class).

[feel free to be non-anonymous, the python community is a very friendly place!]

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

Frederick said...

����������� | ����������� ����� | �����������

����� ���� | ����� ���� | ����������� ����

����� | ����������� ���� | ����� �����

����������� | ����������� ���� | ����������� �����

Ronald said...

Quite useful material, thanks so much for the post.
Regina hotels | Regina hotels | massage happy ending

Anonymous said...

Unfortunately the link to the slides is dead.

martinrmiller said...

For anyone interested, it's still possible to retrieve a copy of the slides using the Internet WayBack Machine with this URL:

> http://web.archive.org/web/20100923120542/http://jackdied.com/jackdied/decos_and_metas_uk.pdf