GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||
GeezerVison is showing www.geeksforgeeks.org — Python Classes and Objects - GeeksforGeeks Python Classes and ObjectsLast Updated : 19 Sep, 2026 A class is a user-defined template for creating objects. It bundles data and functions together, making it easier to manage and use them. When we create a new class, we define a new type of object. We can then create multiple instances of this object type. ![]() Creating a ClassClasses are defined using the class keyword, followed by the class name and a colon. Attributes are variables defined inside class and represent properties of the class. Attributes can be accessed using dot (.) operator (e.g., MyClass.my_attribute).
ObjectAn object is a specific instance of a class. It holds its own set of data (instance variables) and can invoke methods defined by its class. Multiple objects can be created from same class, each with its own unique attributes. Let's create an object from Dog class.
Output bark Explanation: sound attribute is a class attribute. It is shared across all instances of Dog class, so can be directly accessed through instance dog1. Initiate Object with __init__()__init__() method acts as a constructor and is automatically executed when an object is created. It is used to initialize the attributes of the object with the values provided at the time of object creation.
Output Buddy Canine Explanation:
__str__() Method__str__() method allows us to define a custom string representation of an object. By default, when we print an object or convert it to a string using str(), Python uses the default implementation, which returns a string like <__main__.ClassName object at 0x00000123>. Take an example of using __str__() method to provide a readable string output for an object:
Output Buddy is 3 years old. Charlie is 5 years old. Explanation:
Difference Between Class and Object
Comment Explore | ||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |