Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Attributes with method access

  • Use a method (show) to access it.
class Person():
    name = 'Joe'
    print(f'Hello {name}')

    def show(self):
        print(Person.name)

x = Person()          # Hello Joe
x.show()              # Joe
print(x.name)         # Joe
print(Person.name)    # Joe

Person.name = 'Jane'
print(x.name)         # Jane
print(Person.name)    # Jane
x.show()              # Jane

x.name = 'Hilda'      # creating and setting the instance attribute
print(x.name)         # Hilda
print(Person.name)    # Jane

x.show()              # Jane