11

Does there exist special class in python to create empty objects? I tried object(), but it didn't allow me to add fields. I want to use it like this:

obj = EmptyObject()
obj.foo = 'far'
obj.bar = 'boo'

Should I each time(in several independent scripts) define new class like this?

class EmptyObject:
    pass

I use python2.7

7
  • 2
    It's two lines of fairly self-evident code, I see no problem with repeating it. Even if you had to add a new import to obtain an existing EmptyObject, the net savings would be minor. Commented Mar 19, 2014 at 14:46
  • You could even put it on one line, no? Commented Mar 19, 2014 at 14:52
  • 1
    Have you considered namedtuple? I have a sense you're reinventing a wheel. Commented Mar 19, 2014 at 14:53
  • @kojiro What if OP wants to be able to add attributes dynamically, a la JavaScript objects? Commented Mar 19, 2014 at 14:56
  • 1
    @2rs2ts so, like Bunch. Which, personally, I think is a great way to generate problems. Commented Mar 19, 2014 at 14:59

4 Answers 4

10

types.SimpleNamespace was introduced with Python 3.3 to serve this exact purpose. The documentation also shows a simple way to implement it yourself in Python, so you can add it to your pre-Python 3.3 setup and use it as if it was there (note that the actual implementation is done in C):

class SimpleNamespace (object):
    def __init__ (self, **kwargs):
        self.__dict__.update(kwargs)
    def __repr__ (self):
        keys = sorted(self.__dict__)
        items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
        return "{}({})".format(type(self).__name__, ", ".join(items))
    def __eq__ (self, other):
        return self.__dict__ == other.__dict__

But of course, if you don’t need its few features, a simple class Empty: pass does just the same.

Sign up to request clarification or add additional context in comments.

7 Comments

Unfortunately as I've said I use python2.7
@maggot092 And as I have said, you can easily add the implementation to your Python 2 project and use it as if it was there…
In this case as you have also said I don't see advantage comparing defining Emty class
Well, the big advantage would be using a type that does exist in a future Python version, so using an equivalent type will definitely make your intention clear.
The OP was concerned about having to repeat the trivial two lines in each script. Repeating these 9 lines in every script instead appears like a cure that is worse than the disease.
|
8

If you are looking for a place holder object to which you can add arbitrary static members, then the closest I got is an empty lambda function.

obj = lambda: None    # Dummy function
obj.foo = 'far'
obj.bar = 'boo'

print obj.foo, obj.bar
# far boo

Remember: obj is not an object of a class, object doesn't mean class instance, because in Python classes and functions are objects at runtime just like class instances

3 Comments

Why the argument? You could just as well use lambda: 0. I'd not use it still though, as this makes it hard to figure out why you'd create the lambda in the first place.
Mhh... While this is certainly interesting, I sure as hell wouldn't want to see this in production code.
My upvote for originality, but I agree with Lukas Graf, this solution is not for production
7

There is no types.SimpleNamespace in Python 2.7, you could use collections.namedtuple() for immutable objects instead:

>>> from collections import namedtuple
>>> FooBar = namedtuple('FooBar', 'foo bar')
>>> FooBar('bar', 'foo')
FooBar(foo='bar', bar='foo')

Or argparse.Namespace:

>>> from argparse import Namespace
>>> o = Namespace(foo='bar')
>>> o.bar = 'foo'
>>> o
Namespace(bar='foo', foo='bar')

See also, How can I create an object and add attributes to it?

1 Comment

Cool - another reason to use argparse!
2

You can create a new type dynamically with the fields you want it to have using the type function, like this:

x = type('empty', (object,), {'foo': 'bar'})
x.bar = 3
print(x.foo)

This is not entirely what you want though, since it will have a custom type, not an empty type.

1 Comment

Also, it will create a new type on every invocation, which is fairly expensive.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.