posts/Django form fields with the same name as Python keywordsChristine Spanghttp://blog.spang.cc/posts/Django_form_fields_with_the_same_name_as_Python_keywords/blogikiwiki2010-06-04T07:43:33ZMetaclass?http://blog.spang.cc/posts/Django_form_fields_with_the_same_name_as_Python_keywords/comment_1_72d8200e33ea6fd957f18bb5c7f9dfd8/drak.ucw.cz/~bulb//2010-06-04T05:49:14Z2010-06-04T05:49:14Z
I guess if you wanted to do this often, it would make sense to create a metaclass to mangle the member names. Say strip one trailing underscore from all names that have (exactly) one, so you'd declare 'return_' and it would become 'return'. It can be done along the lines of the autoprop example from <a href="http://www.python.org/download/releases/2.2/descrintro/#metaclasses">http://www.python.org/download/releases/2.2/descrintro/#metaclasses</a>. Since python 2.6, the same should possible with class decorator.
comment 2http://blog.spang.cc/posts/Django_form_fields_with_the_same_name_as_Python_keywords/comment_2_79cb24380cdd1c2bd5a9e1e291a9f9be/lamby2010-06-04T07:43:33Z2010-06-04T07:43:33Z
<p>A somewhat nicer way to would be to use the 'type' constructor:</p>
<pre><code>ExampleForm = type('ExampleForm', (forms.Form,), {'return': forms.CharField()})
</code></pre>
<p>That way, it's guaranteed to hit the same codepaths as if you wrote the form out "longhand".</p>