Python Forum
How to access class variable? instances vs class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to access class variable? instances vs class
#2
At first sight, there is no difference: if it doesn't find the raise_amount member in the instance, Python will look for the member in the class, then in the ancester classes if any.

The self.raise_amount is the most robust syntax if you decide to change the structure of the classes later. For example if you change the name of the Employee class, you won't need to change self.raise_amount. Things get even better when you start defining subclasses, for example
class SpecialEmployee(Employee):
    raise_amount = 1.10

jane = SpecialEmployee('Jane', 'Doe', 5000)
jane.apply_raise()
Then Jane's new pay will be 5500 because self.raise_amount will use Jane's class instead of the Employee class.
Reply


Messages In This Thread
RE: How to access class variable? instances vs class - by Gribouillis - Dec-10-2019, 03:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python inner classes inheritance from parent class Abedin 8 791 Apr-23-2025, 05:56 AM
Last Post: Gribouillis
  Accessing method attributes of python class Abedin 6 935 Apr-14-2025, 07:02 AM
Last Post: buran
  Python class members based on a type value voidtrance 7 1,295 Apr-11-2025, 10:10 PM
Last Post: deanhystad
  Create a new subclass in a Python extension based on an existing class voidtrance 6 1,455 Mar-25-2025, 06:37 PM
Last Post: voidtrance
  printing/out put issue with class arabuamir 3 1,021 Aug-25-2024, 09:29 AM
Last Post: arabuamir
  Class test : good way to split methods into several files paul18fr 5 3,868 Jul-17-2024, 11:12 AM
Last Post: felixandrea
  sharepoint: Access has been blocked by Conditional Access policies CAD79 0 2,253 Jul-12-2024, 09:36 AM
Last Post: CAD79
  [split] Class and methods ebn852_pan 15 3,438 May-23-2024, 11:57 PM
Last Post: ebn852_pan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 1,336 May-13-2024, 05:57 AM
Last Post: Larz60+
  Class and methods Saida2024 2 1,138 May-13-2024, 04:04 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020