The Artima Developer Community
Sponsored Link

Python Buzz Forum
Self Take Two

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ian Bicking

Posts: 900
Nickname: ianb
Registered: Apr, 2003

Ian Bicking is a freelance programmer
Self Take Two Posted: Oct 21, 2003 3:14 AM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Ian Bicking.
Original Post: Self Take Two
Feed Title: Ian Bicking
Feed URL: http://www.ianbicking.org/feeds/atom.xml
Feed Description: Thoughts on Python and Programming.
Latest Python Buzz Posts
Latest Python Buzz Posts by Ian Bicking
Latest Posts From Ian Bicking

Advertisement

I presented a sort of Self-like system (in my mind), which I've been extending somewhat. Where that made all classes into instances, why not make all instances into classes? Then we get closer to what Self was doing.

This is done mostly in the metaclass, roughly like this:

class DeclarativeMeta(type):

    def __new__(meta, className, bases, d):
        cls = type.__new__(meta, className, bases, d)
        # We sometimes need access to an instance, not a class,
        # so we create one instance for the future:
        cls.instance = cls.__new__(cls)
        # I don't know quite why we need this, but we do:
        cls.singleton.__init__()
        return cls

    def __call__(cls, **kw):
        """
        Create a subclass of `cls`, given the new class variables
        """
        name = 'Anonymous' + cls.__name__
        newCls = cls.__metaclass__.__new__(
            cls.__metaclass__, name, (cls,), kw)
        return newCls

This way you create new classes with each call, and each class only ever has one instance (it would be preferable if these two could be more closely conjoined, though). The parent/child relationship that Self uses exists through inheritance.

But are we back where we started, just adding some syntactic sugar to otherwise normal Python objects? The difference is that instead of instances belonging to classes, classes stand on their own -- classes are the instances. (Unfortunately we still keep an instance around, but that's just a hack)

I'm actually using this in FormEncode (which I'll release for real someday, really!), and it basically works. I use adaptation to grab the singleton when necessary (since I'm already using adaptation heavily, adding this extra step in is easy).

Read: Self Take Two

Topic: Threads and PyObjc Previous Topic   Next Topic Topic: Now with 10% less content

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use