Building CPython

Marko Rauhamaa marko at pacujo.net
Sat May 16 14:55:55 EDT 2015


Steven D'Aprano <steve+comp.lang.python at pearwood.info>:

> On Sat, 16 May 2015 11:59 pm, Marko Rauhamaa wrote:
>> supports multiple inheritance without classes. Maybe I should port that
>> to Python...
>
> I'd like to see it, but somehow I don't think that your "Scheme object
> system" is another name for "closures". We were talking about closures,
> weren't we?

Ok, here's a quick port that have barely tried out:

========================================================================

### Simple OO Framework

class _O: pass

def make_object(*procedures, base=None, bases=None):
    o = _O()
    methods = {}
    setattr(o, '%methods', methods)
    if base is not None:
        inherit_single(o, base)
    elif bases is not None:
        inherit_multi(o, bases)
    for procedure in procedures:
        methods[procedure.__name__] = procedure
        setattr(o, procedure.__name__, procedure)
    return o

def inherit_single(o, base):
    methods = getattr(o, '%methods')
    for name, method in getattr(base, '%methods').items():
        methods[name] = method
        setattr(o, name, method)

def inherit_multi(o, bases):
    for base in bases:
        inherit_single(o, base)

### Used as follows

def TCPClient():
    def connect(socket_address):
        ...
    return make_object(connect)

def SMTPClient():
    tcp_client = TCPClient()
    def connect(host):
        tcp_client.connect((host, 25))
    def send_message(message):
        ...
    return make_object(send_message, base=tcp_client)

client = SMTPClient()
client.connect('mail.example.com')
========================================================================

> I mean, people had to *debate* the introduction of closures? There were
> three competing proposals for them, plus an argument for "don't add them".
> Some people say closures were added in Java 7, others say closures have
> been there since the beginning, and James Gosling himself says that Java
> used inner classes instead of closures but the result was painful...

I'm with those who say anonymous and named inner classes have been there
forever and serve the purpose of closures. Yes, Java's boilerplate
requirements are painful, but if you don't like that, use Python.

> It seems to me that in the Java community, there's a lot of confusion over
> closures, which are seen as an advanced (and rather scary) feature. Hardly
> routine.

Importantly, anonymous inner classes have been in active use by Java
newbs from day one.


Marko



More information about the Python-list mailing list