Any vs. object

The type object is another type that can have an instance of arbitrarytype as a value. Unlike Any, object is an ordinary static type (itis similar to Object in Java), and only operations valid for _all_types are accepted for object values. These are all valid:

  1. def f(o: object) -> None:
  2. if o:
  3. print(o)
  4. print(isinstance(o, int))
  5. o = 2
  6. o = 'foo'

These are, however, flagged as errors, since not all objects support theseoperations:

  1. def f(o: object) -> None:
  2. o.foo() # Error!
  3. o + 2 # Error!
  4. open(o) # Error!
  5. n = 1 # type: int
  6. n = o # Error!

You can use cast() (see chapter Casts and type assertions) or isinstance() togo from a general type such as object to a more specifictype (subtype) such as int. cast() is not needed withdynamically typed values (values with type Any).