Updated March 23, 2026c
Made with the most excellent and free Pandoc document converter, by converting Markdown to HTML. And Pandoc puts in the Table of Contents too!
Think of variables as objects that can hold one value, like an integer, or hold many values, like a list. A class defines another custom object which is more complex. A class is a blueprint for a variable/object with integers, strings, lists, dictionaries, or more and it can have its own functions called methods. The variable portion of a class object are called properties or attributes, it depends which language you talk about.
A "class object" is a variable of that class. So in a video game we can make a class with these properties: Charactername, strength, intelligence, dexterity, speed, and an image for what they look like. And every character uses the same class but with different values for the variables/properties inside that class.
I use this class to pass options to every function I use. It can hold a lot of data, including large lists and large dictionaries.
import os
import time
#####################################################
# Classes
# Option variables go here.
# Ways to access class members/properties: print(options.debug)
# print(vars(options)['debug'])
# print(getattr(options,'debug'))
class clsOptions: # This is the class blueprint.
r'''
This holds all command line options.
'''
def __init__(self):
r'''These are class variables we need to use with initial values.'''
# Program options
self.debug = False # From --debug
self.debuglist = [] # List of models to debug/break on.
self.optlimitread = 0 # 0 = no limit on reading records.
self.optoverwrite = False # True to overwrite output files, for testing.
self.optcolorchg = 0 # From --colorchg:N
self.starttime = 0 # Used to time how long the program takes to run.
def __repr__(self):
r'''Usage: in debugger show the value of all class members like this: p options
This reduces typing quite a bit.
'''
procname = str(inspect.stack()[0][3]) + ":"
s = f"{procname} This holds command line options.\n"
s = s + f"debug={self.debug}\n"
s = s + f"debuglist={self.debuglist}\n"
s = s + f"optlimitread={self.optlimitread}\n"
s = s + f"optcolorchg={self.optcolorchg}\n"
s = s + f"optoverwrite={self.optoverwrite}\n"
s = s + f"starttime={self.starttime}\n"
return s
# Here we make the instance of a class, the actual variable we use.
options = clsOptions() # Make the class variable/instance.
options.starttime = time.time() # Set a class property
Why is AI often bad to use with programming?
I found this out the hard way. I was asking AI how to do stuff in Python, and didn't mention the Python version. AI was giving me code from 13 years ago that didn't work in modern python. You have to be really specific with your questions. Once I included the Python version in my questions all the code worked.
And I asked it to write a program (a screen saver) using pygame but it never knew pygame was deprecated and replaced with pygame-ce. So my program didn't work in Python 3.14. Pygame wasn't updated for the newer version of Python.
AI seems fine to suggest snippets of code for a task I want to do but it is not good at designing the overall program, especially using proper security for an online web program.
Just find Python tutorials on Youtube. I like the free Indently tutorials. Or use https://freecodecamp.org/. They have a Python tutorial on YT as well.
[\d128-\d255] or
[\x80-\xff][^ -~] (space
through tilde)[ -~]These sites may or may not save your code that you type in. Always have a backup of your code. But they can be used to test small snippets of code or concepts.
Command line: python --version
In code:
import sys
print(sys.version)
# Outputs: '3.11.9 (tags/v3.11.9:de54cf5, Apr 2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)]'
# More details
speedtestname = "Module name"
print(sys.version_info)
if (sys.version_info > (sys.version_info.major, sys.version_info.minor, 0)):
sys.exit(f"ERROR: This module {speedtestname} only supports Python 2.4 - 3.7.")
# Find shorter version like: 3.11.9
import platform
print(platform.python_version()) # Prints '3.11.9'
t = platform.python_version_tuple() # If you want a tuple
print(t) # prints ('3', '11, '9')