---
# System prepended metadata

title: PID-939

---

In Python, we can create anonymous objects using the `type()` function or the `namedtuple` class [2][3][4]. Here's an example of creating an anonymous object using `type()`:

```python
my_obj = type('', (), {'foo': 1, 'bar': 'hello'})

print(my_obj.foo)  # Output: 1
print(my_obj.bar)  # Output: hello
```

In the above example, we create an anonymous object with two attributes `foo` and `bar`. The first argument to `type()` is the name of the class, which we set to an empty string to create an anonymous class. The second argument is a tuple of base classes, which we set to an empty tuple since we don't need any base classes. The third argument is a dictionary of attributes for the class, which we set to `{'foo': 1, 'bar': 'hello'}` to define the attributes of the anonymous object.

Alternatively, we can use the `namedtuple` class to create an anonymous object as follows:

```python
from collections import namedtuple

my_obj = namedtuple('MyObj', ['foo', 'bar'])(1, 'hello')

print(my_obj.foo)  # Output: 1
print(my_obj.bar)  # Output: hello
```

In the above example, we create an anonymous object with two attributes `foo` and `bar`. We use the `namedtuple` class to define the structure of the object, and then create an instance of the object with the values `(1, 'hello')`.

Note that anonymous objects are not commonly used in Python, and it is usually better to define a class explicitly if you need to create objects with specific attributes.