object() function takes no argument and returns a new featureless object—the base and parent of all classes. As such it provides all methods that are common to all Python class instances such as
__repr__() and other “dunder” methods. However, unlike for all non-
object instances, you cannot assign arbitrary attributes to an instance of the object class—this is because of Python’s lack of the method
|Return Value||Returns a new instance of the |
>>> object() <object object at 0x0000020A4201E3A0> >>> help(obj) Help on object object: class object | The most base type
Two objects created with
object() are always unique which makes them an excellent choice for implementing the sentinel pattern:
>>> object() == object() False >>> object() is object() False
Python object() Video
What is the Purpose of Python object()?
The fact that the instance returned by the constructor object() doesn’t provide a __dict__() method implementation means that you cannot add attributes to the object instance. It may seem to you that this makes it essentially useless—you cannot create your custom class with custom methods and attributes. However, the object() function is often used to create a sentinel object or dummy data when None cannot be used—for instance if
None is part of the valid data in a list and you want to iterate until you find the sentinel data
Next, I’ll show you an example of this:
Check out my new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Publisher Link: https://nostarch.com/pythononeliners
So, here’s an example of the use of
object() as sentinel data. We use a single object instance as input to the built-in
iter() function that keeps iterating and ask the user for some input until the user enters
'stop'. Only then does the iterator function
step() return the sentinel “dummy” object that causes the main loop to terminate:
sentinel = object() def step(): user_input = input('your number: ') if user_input == 'stop': return sentinel return user_input for user_input in iter(step, sentinel): print('your input: ', user_input)
One of my test runs turned out like this:
>>> your number: 2 your input: 2 your number: 2 your input: 2 your number: 2 your input: 2 your number: 42 your input: 42 your number: stop
It keeps looping until I put in
'stop'. This causes the function to return the sentinel object which terminates the iterator.
The advantage of using a unique
object() object as a sentinel object compared to
None is that
object() creates a unique object but
None does not.
object() function takes no argument and returns a new featureless object—the base and parent of all classes.
As such it provides all methods that are common to all Python class instances such as
__repr__() and other “dunder” methods.
However, unlike for all non-
object instances, you cannot assign arbitrary attributes to an instance of the object class—this is because of Python’s lack of the “dunder”
>>> obj = object() >>> obj.a = 2 Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> obj.a = 2 AttributeError: 'object' object has no attribute 'a'
Want to keep improving your Python skills? Check out our free Python cheat sheets:
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.