Python Forum
Distinguishing different types of class attributes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Distinguishing different types of class attributes
#5
I don't use class variables very often.

Here I used class variables to define some constants that are used by instances of the class.
1
2
3
4
5
6
7
8
9
10
11
12
class Message:
    """A class for sending and receiving XXXX style messages.
    My most used methods are:
    send_message - Send message to remote process
    recv_ reply - Get reply from sent message
    recv_message - Read asynchronous message from a remote process
    send_reply - Send reply to remote process
    """
    sentinel = 0xF1F2F3F4  # Value used to mark start and end of a message
    max_params = 8  # Number of parameters in message
    typestr_size = 16  # Size for argument typestring in message
    selector_size = 32  # Size for selector string in message
Here I use a class variable to keep a list of all instances of the class.
1
2
3
4
5
6
7
8
9
class Model(XXXX):
    instances = []
 
    def __init__(self, parent=None, link=None):
        super().__init__()
        self.parent = parent
        self._link = link
        self.parts = []
        self.instances.append(self)
I can use Model.instances to count how many instances of the class were created, to execute an instance method for each instance, to delete all instances, to keep all instances from being garbage collected.

I've seen code from other Python programmers who use class variables fairly frequently. I remember one poster on this forum who used classes as namespaces. All the variables in his classes were class variables, and all of his classes were singletons.

Class variables are a language feature and it is really up to you to decide when the are useful. Your problem space is not the same as mine. How I write Python is not how you should write Python.
Reply


Messages In This Thread
RE: Distinguishing different types of class attributes - by deanhystad - Feb-21-2022, 06:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Accessing method attributes of python class Abedin 6 890 Apr-14-2025, 07:02 AM
Last Post: buran
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 1,938 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  Variable Types in a class nafshar 9 4,711 Oct-07-2022, 07:13 PM
Last Post: deanhystad
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,939 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  Calls to Attributes of a Class SKarimi 3 4,624 Apr-22-2021, 04:18 PM
Last Post: SKarimi
  SQL Alchemy dynamic class - declarative_base losing attributes mrdominikku 4 5,026 Jan-10-2020, 06:46 PM
Last Post: mrdominikku
  OpenCV - Distinguishing between three icons kainev 2 2,710 Jul-30-2019, 11:57 PM
Last Post: kainev
  how to add class instance attributes from list 999masks 2 3,374 Jul-22-2019, 07:59 AM
Last Post: 999masks
  Is it possible to loop through class attributes via string? 04chiak 3 11,307 Feb-04-2018, 09:29 PM
Last Post: 04chiak
  Class Instances overriding class members and attributes. Leaf 7 10,963 Nov-29-2017, 06:08 AM
Last Post: Leaf

Forum Jump:

User Panel Messages

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