python - Use of object <type 'object'> -


in python, know there general object type, of every class naturally heir.

if type object in console, returns type <'object'>.

>>> object <type 'object'> 

so far, good. possible instantiate variable of type object

>>> var = object() >>> var <object object @ 0x021684d8> 

my question is:

is there reason object type being instantiable? there use this? or made sake of formality?

there @ least 1 practical reason instantiate object: it's quick, easy, , idiomatic way value not compare equal other value (and raise exception of try ordering comparison). makes perfect sentinel. example:

last = object() value in iterable:     if value != last:         yield value         last = value 

if used none sentinel value, wrong thing if iterable started none, there's no way iterable can start brand-new object instance created.


however, if there no practical use object instances, consider every instance of subclass of object object, conceptually you're creating object instances time--and practically, if you're designing subclass uses super() in __init__ or __new__ no longer conceptual. if object.__init__ unavailable, or raised, you'd have create new do-nothing root class hierarchy of cooperative classes have do-nothing base implementation.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -