[BangPypers] Object Oriented Programming in python

Saager Mhatre saager.mhatre at gmail.com
Tue Oct 22 09:40:30 CEST 2013

On Oct 22, 2013 10:54 AM, "Sirtaj Singh Kang" <sirtaj at sirtaj.net> wrote:
> I agree with you partially - MOP in python can get ugly, but there's
plenty of power there. I'll try to explain, though this stuff is
notoriously hard to articulate (may be just for me).

Oh, trust me, it's not just you! We should start a club, I'm sure we'll
find plenty of members. :)

> So the main role of metaprogramming was to expose the language's object
system as an object model to the programmer, so that it could be queried
and manipulated at runtime. Does python do this? Yes, but not quite in the
way that Kiczales outlined.

Like I said, the reflective metaprogramming nuts and bolts are all there;
it's just that Python expects you to pick up a spanner and put all of them
together every time. No thank you.

BTW, thanks for the Kiczales reference. Added to my reading list.

> Instead we get some rough-and-ready tools with which you can simulate the
effects that a "proper" MOP system (such as in CLOS) might have, and when
having to make a choice between flexibility vs performance and code
clarity, python's designers have almost invariably chosen the latter.

I guess we're going to have to agree to disagree on that last point. I
don't find the MOP implementation in Ruby or Groovy less per for many or
leading to obscure code. On the contrary, I find Python's 'rough-and-ready
tools' make for more confusing metaprogramming code. YMMV

> To begin with, there is a level of metacircularity in the new-style type
> >>> isinstance(object, object)
> True
> >>> isinstance(type, type)
> True
> >>> isinstance(object, type)
> True
> >>> isinstance(type, object)
> True

I think the metacircularity is almost in avoidable if reflective
metaprogramming is a goal. I don't mind it much now that I understand it.

> This is what the metaclass system uses. With old-style classes, you had a
lot of flexibility to change the class of objects at runtime. Nowadays
there are a lot more restrictions, mostly due to performance, but you can
still do some silly stuff:
> >>> MyType = type('MyType', (type,), {})
> >>> MyCircularType = MyType('MyCircularType', (MyType,), {})
> >>> MyCircularType.__class__ = MyCircularType
> >>> isinstance(MyCircularType, MyCircularType)
> True

Ah yes, old-style classes! But I'd be hard pressed to call that MOP as
there are no real metaobjects at play. It's just classes and instances
where instances could have their class swapped out from under them (yikes!)

That's a fun example, though. :)

> And then of course there's things like get/setattribute - combined with
the abc module in the standard library, the type trickery above and
python's basic metaclass system, you could effectively create your own
metacircular MOP protocol on top of python.*

Yeah, Python's fundamental unit being the function causes me some cognitive
dissonance when a lot of the OO utilities (like get/setattr) work
outside-in while others (like the dunder methods) work inside-out. As for
the abc module, the documentation was involved enough to turn me off that
part of the toolkit. But that's another discussion altogether.

Also, you could create/simulate MOP using the tools Python provides you,
but I'd much rather the platform give me a consistent implementation than
have to deal with N people each implementing their own special variant of
what they think MOP should be.

> Another major issue is that python's syntax makes it just not a great
language for embedded DSLs and MOP, even to the extent that other dynamic
languages (Ruby!) are. That's not going to change though, and I'm
personally okay with it for the most part. If I wanted to program CLOS, I'd
program CLOS.

Agreed on the bit about syntax; never delved into CLOS, so I'll stay away
from that bit.

Like I said, I believe Python has a sweet spot and I'm OK using it for
that. It's just that some of these features don't figure in that spot IMHO.

> * In fact, I spent quite a while doing so a few years ago - implementing
the OMG MOF on python - and it worked pretty well, but it proved difficult
to get it to the point where I could even properly explain it to people.

You did... what!?! :O
I'm not sure whether that deserves respect or sympathy! :D

- d

More information about the BangPypers mailing list