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 7613063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:04:35+00:00 2026-05-31T02:04:35+00:00

So, I am reading a bit about metaclasses in Python, and how type() ‘s

  • 0

So, I am reading a bit about metaclasses in Python, and how type()‘s three-argument alter-ego is used to dynamically create classes. However, the third argument is usually a dict that initializes the to-be created class’ __dict__ variable.

If I want to dynamically create classes based on a metaclass that uses __slots__ instead of __dict__, how might I do this? Is type() still used in some fashion along with overriding __new__()?

As an FYI, I am aware of the proper uses for __slots__, to save memory when creating large numbers of a class versus abusing it to enforce a form of type-safety.

Example of a normal (new-style) class that sets __metaclass__ and uses a __dict__:

class Meta(type):
    def __new__(cls, name, bases, dctn):
        # Do something unique ...
        return type.__new__(cls, name, bases, dctn)

class Foo(object):
    __metaclass__ = Meta

    def __init__(self):
        pass

In the above, type.__new__() is called and the fourth argument (which becomes the third when actually used) creates a __dict__ in Foo. But if I wanted to modify Meta to include __slots__, then I have no dictionary to pass on to type()‘s __new__() function (as far as I know — I haven’t tested any of this yet, just pondering and trying to find some kind of a use-case scenario).

Edit: A quick, but untested guess, is to take a dict of the values to be put into the __slots__ variables and pass it to type.__new__(). Then add an __init__() to Meta that populates the __slots__ variables from the dict. Although, I am not certain how that dict would reach __init__(), because the declaration of __slots__ prevents __dict__ from being created unless __dict__ is defined in __slots__…

  • 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-31T02:04:36+00:00Added an answer on May 31, 2026 at 2:04 am

    You can’t create a type with a non-empty __slots__ attribute. What you can do is insert a __slots__ attribute into the new class’s dict, like this:

    class Meta(type): 
        def __new__(cls, name, bases, dctn):
             dctn['__slots__'] = ( 'x', )
             return type.__new__(cls, name, bases, dctn)
    
     class Foo(object):
        __metaclass__ = Meta
    
        def __init__(self):
            pass 
    

    Now Foo has slotted attributes:

    foo = Foo() 
    foo.y = 1
    

    throws

     AttributeError: 'Foo' object has no attribute 'y'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been reading a bit about what metaclasses are, but I would like to
Im skinning a progressBar in Flex, and after reading a bit about it, I
After reading a bit more about how Gnutella and other P2P networks function, I
I've been reading a fair bit about the performance of using LINQ rather than
So I've been reading quite a bit about Unicoding tonight because I was thinking
I'm about to start learning Ruby and would like a bit of reading material
I´m a little bit confused by reading all the posts and tutorials about starting
Been reading a bit about the subject, and out of curiosity, online games which
I've been reading a bit about functional programming recently and am keen to get
I was just reading a bit about the HQ9+ programming language: https://esolangs.org/wiki/HQ9+ , https://en.wikipedia.org/wiki/HQ9+

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.