Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Define Class Attributes

Learn to create class-level attributes shared by all instances.

Python practice8 minClasses & Objects FundamentalsBeginnerLast updated April 7, 2026

Problem statement

Create a class named Dog. Define a class attribute species set to the string 'Canis familiaris'. Implement an __init__ that accepts name and age and sets them as instance attributes (self.name and self.age). Confirm that accessing species from the class or from instances returns the class attribute value, and that assigning a new species to an instance does not change the class attribute.

Task

Define a class attribute and confirm it is accessible from both the class and instances while remaining shared unless overridden on an instance.

Examples

Class and instance access

Input

Dog.species

Output

Canis familiaris

The species attribute is defined at the class level and should be accessible directly from the class.

Input format

Use class attribute access (Dog.species) or instance access (Dog('Fido', 3).species).

Output format

Return the class attribute value or instance attribute values as appropriate.

Constraints

Name is a string. Age is an integer.

Samples

Sample 1

Input

Dog('Rex', 2).name

Output

Rex

Instance attribute name should be set by the constructor.