ExampleEdit & Run
#Use the setattr Function

class Example:
   a = "Pynerds"

#set a variable attribute to the class
setattr(Example, "b", "Python")
setattr(Example, "c", "Django")

def example_method():
    print("Hello, World!")

#set a method attribute to the class
setattr(Example, "example_method", example_method)

print(Example.a)
print(Example.b)
print(Example.c)

Example.example_method()
copy
Output:
Pynerds Python Django Hello, World! [Finished in 0.011267599184066057s]

The setattr() function is used to  dynamically set the value of an object's attribute. This function is most commonly used for adding a new attribute to an instance of a class, but can also be used with any other type of object. It can be used as an alternative to attribute assignment ( i.e MyClass.some_attribute = value)

Syntax:
setattr(obj, name, value)
copy

obj

Specifies the object on which to set the attribute.
name Specifies the name of the attribute to be set.

value

Specifies value of the attribute to be set.
Parameters:

The function sets the attribute to the specified object and returns None. If an attribute already exists with a similar name, its value will be overwritten.

ExampleEdit & Run
class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age 

stu = Student('John', 20)

print("Name:", stu.name)
print("Age:", stu.age)

# Setting the attribute value
setattr(stu, 'name', 'David')

print("Name:", stu.name)
copy
Output:
Name: John Age: 20 Name: David [Finished in 0.009923504665493965s]
ExampleEdit & Run
class Person():
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname

def _str(self):
   return f'my name is {self.fname} {self.lname}'

setattr(Person, '__str__', _str)

p = Person('John', 'Doe')
print(p)
copy
Output:
my name is John Doe [Finished in 0.009855836164206266s]

The setattr function may be  helpful  in some cases, however, It should be used only when necessary.  Using the function unnecessarily can lead to debugging issues.It can also be slow, as it has to traverse the class hierarchy to find the attribute being set. 

Whenever possible,  it is more preferable to use a more explicit approach when setting class attributes such as creating attributes in the class definition and using assignment to set values.