class vs type

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Oct 20 00:47:31 EDT 2007


On Fri, 19 Oct 2007 12:21:25 -0400, Colin J. Williams wrote:

> In this case, why do we continue to use the word class to generate a
> type?

Because type is a callable, and class is convenient syntactic sugar.

class Parrot(object):
    def speak(self, msg):
        return "Polly sez %s" % msg


is much more convenient than:

import new
Parrot = type('Parrot', (object,), 
    {'speak': lambda self, msg: 'Polly sez %s' % msg})

particularly for large classes with many methods.



-- 
Steven



More information about the Python-list mailing list