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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:32:55+00:00 2026-06-09T19:32:55+00:00

On a regular basis, I want to create completely empty objects in Python. Basically,

  • 0

On a regular basis, I want to create completely empty objects in Python. Basically, I want to have a dict where the keys are attributes. In Python 2.x, I can create an old-style type like this:

class Empty: pass

This will create a type that only has two attributes (__doc__ and __module__). In Python 3.x, everything is a new-style class, so I get 18 attributes.

In my current situation, I have a class that allows you to specify types that need monkey patched within a unit test. When the patches are applied, I am creating a type with attributes with the names of each mocked-out type. This is pretty much what my current implementation is doing:

class EmptyType: pass
...
mocks = EmptyType()
for mock_name, mock in patches:
    setattr(mocks, mock_name, mock)

My concern is that should someone be mocking a private member, they might run into naming collisions with the names in the EmptyType object. That’s why I’d like to keep as few attributes in the EmptyType as possible. And it is way easier to say mocks.find_users than it is to say mocks["find_users"], especially when I know the name has to be a valid identifier.

Right now, I have provided the ability to give mocks different names, other than what would otherwise be the default. Still, it would be nice to avoid confusing errors in the first place. It is very easy to create almost empty types in JavaScript and I was hoping there was something similar in Python, since I keep finding good uses for them.

  • 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-06-09T19:32:57+00:00Added an answer on June 9, 2026 at 7:32 pm

    What about creating your own custom container?

    class Empty(object):
    
        def __init__(self, **kwargs):
            object.__setattr__(self, '_obj', kwargs)
    
        def __getattribute__(self, name):
            obj = object.__getattribute__(self, '_obj')
            try:
                return obj[name]
            except KeyError:
                cls_name = object.__getattribute__(self, '__class__').__name__
                raise AttributeError(
                    "'%(cls_name)s' object has no attribute '%(name)s'" % locals())
    
        def __setattr__(self, name, val):
            obj = object.__getattribute__(self, '_obj')
            obj[name] = val
    
        def __getitem__(self, key):
            return getattr(self, key)
    
        def __setitem__(self, key, val):
            return setattr(self, key, val)
    

    Usage:

    e = Empty(initial='optional-value')
    e.initial
    # 'optional-value'
    e.val = 'foo'
    e.val
    # 'foo'
    e.bad
    # AttributeError: 'Empty' object has no attribute 'bad'
    setattr(e, 'user', 'jdi')
    e.user
    # 'jdi'
    e['user']
    # 'jdi'
    
    # note that you dont even see anything when you dir()
    dir(e)
    # []
    
    # and trying to access _obj is protected
    e._obj
    #AttributeError: 'Empty' object has no attribute '_obj'
    
    # But you could even set your own _obj attribute
    e._obj = 1
    e._obj
    # 1
    

    It will store everything under a _obj dictionary so you basically get a clean space that doesn’t conflict with the actual instance attributes.

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

Sidebar

Related Questions

How can I access a user session objects from another thread? I want to
I have table in mysql database with autoincrement PRIMARY KEY. On regular basis rows
I want to monitor a user's location on a regular basis, like Google Latitude
I have traders that are evaluated on a regular basis. The evaluations have a
I am going to create an application which will have a regular web interface
I have a background worker which updates the GUI on a regular basis via
I am new to working with Spring on regular basis so I had a
Pretty much all the apps I use on a regular basis implement this 'seemly
Indexing, code completion, & coloring disappear from my projects on a fairly regular basis,
Regular Expressions are usually expressed as strings, but they also have properties (ie. single

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.