Creating an empty object in Python

PythonOop

Python Problem Overview


Are there any shortcuts for defining an empty object in Python or do you always have to create an instance of a custom empty class?

Edit: I mean an empty object usable for duck typing.

Python Solutions


Solution 1 - Python

You can use type to create a new class on the fly and then instantiate it. Like so:

>>> t = type('test', (object,), {})()
>>> t
<__main__.test at 0xb615930c>

The arguments to type are: Class name, a tuple of base classes, and the object's dictionary. Which can contain functions (the object's methods) or attributes.

You can actually shorten the first line to

>>> t = type('test', (), {})()
>>> t.__class__.__bases__
(object,)

Because by default type creates new style classes that inherit from object.

type is used in Python for metaprogramming.

But if you just want to create an instance of object. Then, just create an instance of it. Like lejlot suggests.

Creating an instance of a new class like this has an important difference that may be useful.

>>> a = object()
>>> a.whoops = 1
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'whoops'

Where as:

>>> b = type('', (), {})()
>>> b.this_works = 'cool'
>>> 

Solution 2 - Python

Yes, in Python 3.3 SimpleNamespace was added

> Unlike object, with SimpleNamespace you can add and remove attributes. If a SimpleNamespace object is initialized with keyword arguments, those are directly added to the underlying namespace.

Example:

import types

x = types.SimpleNamespace()
x.happy = True

print(x.happy) # True

del x.happy
print(x.happy) # AttributeError. object has no attribute 'happy'

Solution 3 - Python

One simple, less-terrifying-looking way to create an empty(-ish) object is to exploit the fact that functions are objects in Python, including Lambda Functions:

obj = lambda: None
obj.test = "Hello, world!"

For example:

In [18]: x = lambda: None

In [19]: x.test = "Hello, world!"

In [20]: x.test
Out[20]: 'Hello, world!'

Solution 4 - Python

You said it in the question, but as no answer mentioned it with code, this is probably one of the cleanest solutions:

class Myobject:
    pass

x = Myobject()
x.test = "Hello, world!"  # working

Solution 5 - Python

What do you mean by "empty object"? Instance of class object? You can simply run

a = object()

or maybe you mean initialization to the null reference? Then you can use

a = None

Solution 6 - Python

You can use

x = lambda: [p for p in x.__dict__.keys()]

Then

x.p1 = 2
x.p2 = "Another property"

After

x()
# gives
# ['p1', 'p2']

And

[(p, getattr(x,p)) for p in x()]
# gives
# [('p1', 2), ('p2', 'Another property')]

Solution 7 - Python

All the proposed solutions are somewhat awkward.

I found a way that is not hacky but is actually according to the original design.

>>> from mock import Mock
>>> foo = Mock(spec=['foo'], foo='foo')
>>> foo.foo
'foo'
>>> foo.bar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../virtualenv/local/lib/python2.7/site-packages/mock/mock.py", line 698, in __getattr__
    raise AttributeError("Mock object has no attribute %r" % name)
AttributeError: Mock object has no attribute 'bar'

See the documentation of unittest.mock here.

Solution 8 - Python

Constructs a new empty Set object. If the optional iterable parameter is supplied, updates the set with elements obtained from iteration. All of the elements in iterable should be immutable or be transformable to an immutable using the protocol described in section Protocol for automatic conversion to immutable.

Ex:

myobj = set()
for i in range(1,10): myobj.add(i)
print(myobj)

Solution 9 - Python

In my opinion, the easiest way is:

def x():pass
x.test = 'Hello, world!'

Solution 10 - Python

If there is a desired type of the empty object, in other words, you want to create it but don't call the __init__ initializer, you can use __new__:

class String(object):
    ...

uninitialized_empty_string = String.__new__(String)

Source: https://stackoverflow.com/a/2169191/6639500.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSmilesView Question on Stackoverflow
Solution 1 - PythonaychedeeView Answer on Stackoverflow
Solution 2 - PythonVlad BezdenView Answer on Stackoverflow
Solution 3 - PythonWillView Answer on Stackoverflow
Solution 4 - PythonBasjView Answer on Stackoverflow
Solution 5 - PythonlejlotView Answer on Stackoverflow
Solution 6 - PythonBrault GilbertView Answer on Stackoverflow
Solution 7 - PythonqbenView Answer on Stackoverflow
Solution 8 - PythonD zView Answer on Stackoverflow
Solution 9 - PythonRastersoftView Answer on Stackoverflow
Solution 10 - PythonSerVBView Answer on Stackoverflow