Feb-21-2022, 06:34 PM
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.
Here I use a class variable to keep a list of all instances of the class.
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.
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 |
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'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.