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
IT Ninja
  • 6,174
  • 10
  • 42
  • 65

1 Answers1

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).

Amber
  • 507,862
  • 82
  • 626
  • 550