0% found this document useful (0 votes)
23 views

Python Object Class

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Python Object Class

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Introduction to the

object Superclass
python
• object is a built-in Python class that is the ancestor of all other classes.
• Every class in Python inherits from object.
• The object class provides default implementations for special methods.
• You can override these methods to customize class behavior.
• Why It Matters:
• Understanding that object is the base class helps in grasping how inheritance
and special methods work in Python.
Special Methods Provided by object
• __init__:
• Purpose: Initializes a new instance of a class.
• Example

• Note: By default, object.__init__() does nothing. If you define __init__


in your class, you are overriding this default behavior.
• __str__:
• Purpose: Provides a string representation of the object, used by
print() and str().
• Example:

• Note: The default __str__ implementation shows the class name and
memory location.
• __eq__:
• Purpose: Determines if two objects are equal (used by ==).
• Example

• Note: By default, __eq__ compares object identities (i.e., is).


Overriding Special Methods

You might also like