Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 917499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:13:20+00:00 2026-05-15T18:13:20+00:00

I think I finally figured out they need to use this DeclarativeFieldsMetaclass (to turn

  • 0

I think I finally figured out they need to use this DeclarativeFieldsMetaclass (to turn the class fields into instance variables and maintain their order with an ordered/sorted dict). However, I’m still not quite sure why they opted to use a BaseForm rather than implementing everything directly within the Form class?

They left a comment,

class Form(BaseForm):
    "A collection of Fields, plus their associated data."
    # This is a separate class from BaseForm in order to abstract the way
    # self.fields is specified. This class (Form) is the one that does the
    # fancy metaclass stuff purely for the semantic sugar -- it allows one
    # to define a form using declarative syntax.
    # BaseForm itself has no way of designating self.fields.

But I don’t really understand it. “In order to abstract the way self.fields is specified” — but Python calls DeclarativeFieldsMetaclass.__new__ before Form.__init__, so they could have taken full advantage of self.fields inside Form.__init__ as is; why do they need an extra layer of abstraction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:13:21+00:00Added an answer on May 15, 2026 at 6:13 pm

    Source:

    class MetaForm(type):
        def __new__(cls, name, bases, attrs):
            print "%s: %s" % (name, attrs)
            return type.__new__(cls, name, bases, attrs)
    
    class BaseForm(object):
        my_attr = 1
        def __init__(self):
            print "BaseForm.__init__"
    
    class Form(BaseForm):
        __metaclass__ = MetaForm
        def __init__(self):
            print "Form.__init__"
    
    class CustomForm(Form):
        my_field = 2
        def __init__(self):
            print "CustomForm.__init__"
    
    f = CustomForm()
    

    Output:

    Form: {'__module__': '__main__', '__metaclass__': <class '__main__.MetaForm'>, '__init__':<function __init__ at 0x0227E0F0>}
    CustomForm: {'__module__': '__main__', 'my_field': 2, '__init__': <function __init__ at 0x0227E170>}
    CustomForm.__init__
    

    Looks like MetaForm.__new__ is called twice. Once for Form and once for CustomForm, but never for BaseForm. By having a clean (empty) Form class, there won’t be any extraneous attributes to loop over. It also means that you can define Fields inside the BaseForm that could be used for internal use, but avoid rendering.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have finally figured out how to use the apple Reachability files, which is
I finally figured out a good/easy way to make clean URLs with regex on
I finally figured out what's wrong with my code, but I'm not sure how
EDIT: I used, finally, inotify. As stefanB says, inotify is the thing to use.
Think: tiling my emacs window with eshells, a la xmonad. Is this possible? I
I think know how to do this in C# but I'm having syntax trouble
I think I'm burnt out, and that's why I can't see an obvious mistake.
I think I already know the answer to this but thought I would ask
This problem has been plaguing me for a little while, but I've think I've
I'm having difficultly figuring out how to write this block of code... function myFunction(x,y,z)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.