Attributes of class

Variables that are defined at the class level are called attributes.

class Person:
    name = 'Alex'
    age = 10

name and age are class attributes.

Example:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

person = Person("John", 30)
setattr(person, "email", "john@example.com")
print(person.email)  # john@example.com

name = getattr(person, "name")
print(name)  # John

delattr(person, "age")
print(person.age)

Comments (0)

Leave a comment