PEP 359 – The “make” Statement | peps.python.org
PEP 359 – The “make” Statement PEP 359 – The “make” Statement Author : Steven Bethard <steven.bethard at gmail.com> Status : Withdrawn Type : Standards Track Created : 05-Apr-2006 Python-Version : 2.6 Post-History : 05-Apr-2006, 06-Apr-2006, 13-Apr-2006 Table of Contents Abstract Withdrawal Notice Motivation Example: simple namespaces Example: GUI objects Example: custom descriptors Example: property namespaces Example: interfaces Specification Open Issues Keyword The make-statement as an alternate constructor Customizing the dict in which the block is executed Optional Extensions Remove the make keyword Removing __metaclass__ in Python 3000 Removing class statements in Python 3000 References Copyright Abstract This PEP proposes a generalization of the class-declaration syntax, the make statement.
The proposed syntax and semantics parallel the syntax for class definition, and so: make < callable > < name > < tuple > : < block > is translated into the assignment: < name > = < callable > ( "<name>" , < tuple > , < namespace > ) where <namespace> is the dict created by executing <block> . This is mostly syntactic sugar for: class <name> <tuple>: __metaclass__ = <callable> <block> and is intended to help more clearly express the intent of the statement when something other than a class is being created.
Of course, other syntax for such a statement is possible, but it is hoped that by keeping a strong parallel to the class statement, an understanding of how classes and metaclasses work will translate into an understanding of how the make-statement works as well. The PEP is based on a suggestion [1] from Michele Simionato on the python-dev list. Withdrawal Notice This PEP was withdrawn at Guido’s request [2] . Guido didn’t like it, and in particular didn’t like how the property use-case puts the instance methods of a property at a different level than other instance methods and requires fixed names for the property functions.
Motivation Class statements provide two nice facilities to Python: They execute a block of statements and provide the resulting bindings as a dict to the metaclass. They encourage DRY (don’t repeat yourself) by allowing the class being created to know the name it is being assigned. Thus in a simple class statement like: class C ( object ): x = 1 def foo ( self ): return 'bar' the metaclass ( type ) gets called with something like: C = type ( 'C' , ( object ,), { 'x' : 1 , 'foo' : < function foo at ...> }) The class statement is just syntactic sugar for the above assignment statement, but clearly a very useful sort of syntactic sugar.
It avoids not only the repetition of C , but also simplifies the creation of the dict by allowing it to be expressed as a series of statements. Historically, type instances (a.k.a. class objects) have been the only objects blessed with this sort of syntactic support. The make statement aims to extend this support to other sorts of objects where such syntax would also be useful. Example: simple namespaces Let’s say I have some attributes in a module that I access like: mod . thematic_roletype mod . opinion_roletype mod . text_format mod . html_format and since “Namespaces are one honking great idea”, I’d like to be able to access these attributes instead as: mod . roletypes . thematic mod . roletypes . opinion mod . format . text mod . format . html I currently have two main options: Turn the module into a package, turn roletypes and format into submodules, and move the attributes to the submodules.
Create roletypes and format classes, and move the attributes to the classes. The former is a fair chunk of refactoring work, and produces two tiny modules without much content. The latter keeps the attributes local to the module, but creates classes when there is no intention of ever creating instances of those classes. In situations like this, it would be nice to simply be able to declare a “namespace” to hold the few attributes.
With the new make statement, I could introduce my new namespaces with something like: make namespace roletypes : thematic = ... opinion = ... make namespace format : text = ... html = ... and keep my attributes local to the module without making classes that are never intended to be instantiated. One definition of namespace that would make this work is: class namespace ( object ): def __init__ ( self , name , args , kwargs ): self . __dict__ . update ( kwargs ) Given this definition, at the end of the make-statements above, roletypes and format would be namespace instances.
Example: GUI objects In GUI toolkits, objects like frames and panels are often associated with attributes and functions. With the make-statement, code that looks something like: root = Tkinter . Tk () frame = Tkinter . Frame ( root ) frame . pack () def say_hi (): print "hi there, everyone!" hi_there = Tkinter . Button ( frame , text = "Hello" , command = say_hi ) hi_there . pack ( side = Tkinter . LEFT ) root . mainloop () could be rewritten to group the Button’s function with its declaration: root = Tkinter .
Tk () frame = Tkinter . Frame ( root ) frame . pack () make Tkinter . Button hi_there ( frame ): text = "Hello" def command (): print "hi there, everyone!" hi_there . pack ( side = Tkinter . LEFT ) root . mainloop () Example: custom descriptors Since descriptors are used to customize access to an attribute, it’s often useful to know the name of that attribute. Current Python doesn’t give an easy way to find this name and so a lot of custom descriptors, like Ian Bicking’s setonce descriptor [3] , have to hack around this somehow.
With the make-statement, you could create a setonce attribute like: class A ( object ): ... make setonce x : "A's x attribute" ... where the setonce descriptor would be defined like: class setonce ( object ): def __init__ ( self , name , args , kwargs ): self . _name = '_setonce_attr_ %s ' % name self . __doc__ = kwargs . pop ( '__doc__' , None ) def __get__ ( self , obj , type = None ): if obj is None : return self return getattr ( obj , self . _name ) def __set__ ( self , obj , value ): try : getattr ( obj , self . _name ) except AttributeError : setattr ( obj , self . _name , value ) else : raise AttributeError ( "Attribute already set" ) def set ( self , obj , value ): setattr ( obj , self . _name , value ) def __delete__ ( self , obj ): delattr ( obj , self . _name ) Note that unlike the original implementation, the private attribute name is stable since it uses the name of the descriptor, and therefore instances of class A are pickleable.
Example: property namespaces Python’s property type takes three function arguments and a docstring argument which, though relevant only to the property, must be declared before it and then passed as arguments to the property call, e.g.: class C ( object ): ... def get_x ( self ): ... def set_x ( self ): ... x = property ( get_x , set_x , "the x of the frobulation" ) This issue has been brought up before, and Guido [4] and others [5] have briefly mused over alternate property syntaxes to make declaring properties easier.
With the make-statement, the following syntax could be supported: class C ( object ): ... make block_property x : '''The x of the frobulation''' def fget ( self ): ... def fset ( self ): ... with the following definition of block_property : def block_property ( name , args , block_dict ): fget = block_dict . pop ( 'fget' , None ) fset = block_dict . pop ( 'fset' , None ) fdel = block_dict . pop ( 'fdel' , None ) doc = block_dict . pop ( '__doc__' , None ) assert not block_dict return property ( fget , fset , fdel , doc ) Example: interfaces Guido [6] and others have occasionally suggested introducing interfaces into python.
Most suggestions have offered syntax along the lines of: interface IFoo : """Foo blah blah""" def fumble ( name , count ): """docstring""" but since there is currently no way in Python to declare an interface in this manner, most implementations of Python interface
The rest of this page is on the live site.