[BangPypers] Object Oriented Programming in python

Saager Mhatre saager.mhatre at gmail.com
Mon Oct 21 11:32:27 CEST 2013


On Oct 21, 2013 12:09 PM, "Dhananjay Nene" <dhananjay.nene at gmail.com> wrote:
>
> On Mon, Oct 21, 2013 at 11:53 AM, Saager Mhatre <saager.mhatre at gmail.com>
wrote:
> > On Oct 21, 2013 11:39 AM, "Dhananjay Nene" <dhananjay.nene at gmail.com>
wrote:
> >>
> >> On Mon, Oct 21, 2013 at 10:55 AM, Saager Mhatre <
saager.mhatre at gmail.com>
> > wrote:
> >>
> >> > Which generally lead to poor (or at least poorer) abstractions; but I
> > digress.
> >>
> >> Leaky ??  :)
> >
> > For the most part, yes.
> >
> >> >
> >> >> I think OOPs concepts across a number of languages are quite
different.
> >> >> You will find python having superior constructs eg. metaclasses etc.
> > if you were comparing Python OOP to C++/Java.
> >> >
> >> > Superior constructs implemented inferiorly. Meteclasses are much^3
more
> > powerful in Groovy, Ruby and SmallTalk (where some would claim Python
> > borrowed them from; but that's just not true.)
> >>
> >> I wonder if you meant syntactically/stylistically. Would be keen to
> > learn, if there are examples where ruby / groovy (I don't know much
about
> > smalltalk) allow things that python does not.
> >
> > Semantically! MetaClasses are a much more powerful construct in those
> > languages.
>
> I'm specifically looking for evidence to support that. And I suspect
> it might be out there to be found , just that I haven't so far. One of
> my early attempts was documented here
>
http://blog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/
>

Don't look for metaprogramming, you'll end up with a lot of ancillary
stuff. Look for metaclasses or metaobject protocol.

> > They form the core of the MetaObjectProtocol which governs the
> > dynamic dispatch of messages/methods. Modifications to MetaClasses
> > percolate to Classes and objects they are associated with and such
> > modifications as well as MetaClass associations can be dynamic as well
as
> > temporary; leading to some seriously powerful use cases.
>
> The "temporary" is intriguing. I don't quite yet understand what it
> means .. does it mean you could say temporarily add a method and then
> take it out later (or some other similar capability) ?

Yup! Look for Categories. The Groovy guides have a bunch of examples.
(Nothing to do with category theory AFAIK)

> >
> > That's pretty much what always foiled my attempts at understanding
Python
> > MetaClasses, I was looking for power where there was none to find. The
best
> > comparison I could find was to Groovy's Compile time AST transforms, but
> > even those are even more powerful as they drop down a level of
abstraction
> > and hand you the AST for the an rated element.
>
> AST transforms are sort of feasible but rarely done, and not sure if I
> would like to be a part of such an exercise. They kick in via the
> import hook, thus you build the AST on your own, thankfully python
> does provide AST helpers. Googling for python macros will show some
> such valiant attempts - https://github.com/lihaoyi/macropy or
> https://code.google.com/p/metapython/wiki/Tutorial

While I haven't implemented any yet, Groovy's AST transforms make
implementation as well as application fairly easy.

But, that wasn't my point. What I meant was that Python's meta classes only
kick in at module/lass load time whereas Metaclasses in the other languages
play a greater role at runtime.

- d


More information about the BangPypers mailing list