1

Does the object keyword have any uses except for classes like:

class HelloWorld(object):
    pass

Because when assigning a variable to object it seems as if there is no use for it (no variable.__dict__ or anything).

for example:

x=object()
x.anything="foo"#<----- throws an error
2
  • 1
    Just to clarify, its not a keyword, but a base class - almost like an interface, that all objects implicitly inherit from. Commented Oct 23, 2012 at 4:07
  • 1
    Explained here, stackoverflow.com/questions/4015417/… Commented Oct 23, 2012 at 4:10

1 Answer 1

3

One possible use is for sentinel values - things that you want to have unique values that can't be duplicated by accident. For instance:

NOT_SET = object()

def some_function(arg=NOT_SET):
    if arg is NOT_SET:
        # the user didn't pass in a value for 'arg'
     else:
        # the user passed in a value for 'arg'

Because every object instance has a unique identity, the if statement's condition will only be true if the user didn't pass in a value (unlike the common default value for a parameter, None, which the user might pass in to some functions).

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.