I have a rather large class variable with at least 50 attributes. There are more but when I print the class to the screen I only want to print about 50 attributes.
The shortened class definition looks a bit like this.
class clsOptions: # Command line options
def __init__(self):
self.adminemail = 'xxx'
self.debug = False
self.debugprint = False # True to show debug messages.
def __repr__(self):
procname = str(inspect.stack()[0][3]) + ":"
s = f"{procname} This holds command line options.\n"
s = s + f"adminemail={self.adminemail}\n"
s = s + f"debug={self.debug}\n"
s = s + f"debugprint={self.debugprint}\n"
return s
def __str__(self):
r'''Used with print(options)'''
s = f"This is __str__\n"
__repr__(self)
return s
options = clsOptions()
In the debugger I will sometimes issue the command p options to show the value of attributes.
How do I paginate the output so I see one screen of output at a time?
Options
In the class __repr__ method I suppose I could sent the string to a temporary text file then do something like os.system('more tempfile.txt') but that seems awkward. Is there a better way?
for added flexibility for the special case when you would want to enter values at instantiation other than default values?
Can you sub-divide this super class into sub-classes for attributes that are similar and then use inheritance / composition for better maintainability? Fifty attributes seems a bit extreme.
Because my sample for this post is a rather simplified example of what I actually have in the class. What I actually have are about 8-9 class properties which are initialized on class creation, and those initialized values do not vary as I only have one instance of the class in my program.
Can you sub-divide this super class into sub-classes for attributes that are similar and then use inheritance / composition for better maintainability?
You probably should before continuing to work with a class with 50+ attributes. But really, this isn’t about subclasses (which involves inheritance) but just about breaking one class into smaller independent classes, each of which carry some of the 50 attributes you currently have. For example, instead of defining
class Address:
street: str
city: str
zip: str
state: str
class PhoneNumber:
areacode: str
number: str
class Person:
name: str
add: Address
phone: PhoneNumber
Aside from shielding Person from the details of an address or a phone number, imagine what each version of Person would look like if a Person had multiple addresses (home, work, etc) and phone numbers (home, work, mobile, etc).
Honestly, using the more command is the easiest way to go. Linux, macOS, and Windows all have a more command (and named “more”) and more handles stuff like terminal window size and keyboard key presses, which is a lot of work if you’re going to reinvent the wheel.