Class Properties in Python
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Class Properties in Python

Kishore V

Class Properties in Python

Properties are variables that store data inside a class or its objects. Each object created from a class can have its own set of properties.

Defining Properties in a Class

Most properties are defined inside the __init__() method. These are called instance properties because they belong to individual objects.

Example: Create a Class with Properties

101 45000

Accessing Object Properties

You can access properties using dot notation: object_name.property_name

Example: Access Properties

Samsung 30000

Modifying Properties

Object properties can be updated after the object is created.

Example: Update a Property Value

80 95

Deleting Properties

You can remove a property from an object using the del keyword.

Example: Delete a Property

Neo

Class Properties vs Instance Properties

Python supports two types of properties:

[Image contrasting Class Properties shared among all instances versus Instance Properties unique to each object]
Property Type Description
Instance Properties Defined inside __init__() and are unique to each object.
Class Properties Defined directly inside the class body and are shared by all objects.

Example: Class vs Instance Property

Dog Cat Mammal Mammal

Modifying Class Properties

When a class property is modified using the class name, the change affects all objects created from that class.

Example: Update a Class Property

USA USA

Adding New Properties to Objects

Python allows you to add new properties to objects dynamically, even after they have been created. This newly added property will only exist for that specific object.

Example: Add Properties at Runtime

Rahul rahul@example.com 1200
Our website uses cookies to enhance your experience. Learn More
Accept !