Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an empty object in Python

Tags:

python

oop

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.

like image 768
Smiles Avatar asked Oct 20 '13 11:10

Smiles


People also ask

Can I create empty object in Python?

In Python, to write an empty class pass statement is used. pass is a special statement in Python that does nothing. It only works as a dummy statement. However, objects of an empty class can also be created.

Is it possible to create an empty object?

C++ allows creating an Empty class, yes! We can declare an empty class and its object. The declaration of Empty class and its object are same as normal class and object declaration.

What is the Python code to create an empty?

Write a Python program to create a set. To create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.

Can we create object without class in Python?

We already know that an object is a container of some data and methods that operate on that data. In Python, an object is created from a class. To create an object, you have to define a class first.


9 Answers

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'
>>> 
like image 68
aychedee Avatar answered Sep 29 '22 05:09

aychedee


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'
like image 25
Vlad Bezden Avatar answered Sep 29 '22 07:09

Vlad Bezden


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!'
like image 22
Will Avatar answered Sep 29 '22 05:09

Will


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

like image 27
Basj Avatar answered Sep 29 '22 07:09

Basj


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
like image 33
lejlot Avatar answered Sep 29 '22 07:09

lejlot


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.

like image 24
qben Avatar answered Sep 29 '22 06:09

qben


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')]
like image 44
Brault Gilbert Avatar answered Sep 29 '22 07:09

Brault Gilbert


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)
like image 30
D z Avatar answered Sep 29 '22 06:09

D z


In my opinion, the easiest way is:

def x():pass
x.test = 'Hello, world!'
like image 27
Rastersoft Avatar answered Sep 29 '22 06:09

Rastersoft