On Wed, Mar 11, 2009 at 5:34 PM, Chouser <chou...@gmail.com> wrote: [...]
> > Defining a instance method for a Python class allows you to connect > some code to your data, which internally uses a type pointer from the > instance to the class. In Clojure you can put functions directly in > the metadata (as clojure.zip does), or put a type tag in the map or in > the metadata, and use a multimethod dispatching on that to connect > code to your data. > > Similarly, any inheritance in Clojure would normally be defined on a > keyword (or symbol or collection of either) that is in the map or the > map's metadata. In Python, the object knows its class, and the class > knows about the hierarchy. > > I don't know if that leads to any particular conclusion. I suppose it > does suggests a trivial program (or a trivial part of a program) in > Clojure will likely have less code for setting up classes than the > Python equivalent -- you start with the data you actually need, and > can add "methods", polymorphism, etc. if needed later. > I think it's largely possible to abuse Python to achieve some of the possibilities you mentioned. At runtime you can add new methods to a class, you can add new methods directly to an object (hint: use new.instancemethod), you can change the __bases__ of a given class to inject behavior, and you can change the class of an object by assigning to __class__. I included a small example of using the above techniques that makes it easy (I think) to separate code and data in Python by composing instances that provide data with classes that provide behavior at runtime. http://gist.github.com/77848 -- Cosmin Stejerean http://offbytwo.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---