I am new to python and its OOP concepts, I was following below code and got little confused,
class Person:
species = 'Homo sapiens'
def __init__(self, name, age):
self.name= name
self.age = age
p = Person("ABC", 18)
p.species = 'No homo sapien'
print(p.species)
# prints --> No homo sapien
I haven't accessed or manipulated the value of class attribute species by just doing p.species
so it should have thrown some error about species
attribute, but got no error instead it did print.
p.__class__.species = 'Not found'
print("ABC is a {}".format(p.__class__.species))
# error --> SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
This statement threw error as expected. So I want clarification regarding first statement. And can we change class attribute value?