Search:

PmWiki

pmwiki.org

edit SideBar

Main / Python

Python Version Woes

Running with the system Python and libraries limits you to one specific Python version, chosen by your OS provider. Trying to run all Python applications on one Python installation makes it likely that version conflicts will occur among the collection of libraries. It's also possible that changes to the system Python will break other OS features that depend on it.

Nifty tool to analyze what version a Python application needs: https://github.com/ghewgill/pyqver/tree/master

Python Usage

main() function

Python scripts require an explicit call to the main function to execute the code within it. Unlike some other programming languages, Python doesn't automatically run a main function. In your .py file, the left-indented code just runs like a script. To execute the main function, you can add the following code block at the end of your script:

def main():
    print("Hello, world!")

if __name__ == "__main__":
    main()

super() function

Basically allows re-direction of method calls, such as accessing inherited methods that have been overridden. For example, if you create a child class but want to use the parent method in addition to extending it with the child method of the same name, you could use super(Child, self).method().

https://docs.python.org/2/library/functions.html?highlight=super#super
https://rhettinger.wordpress.com/2011/05/26/super-considered-super/

Module Names

In Python, a module is often synonymous with a .py file that contains a collection of related classes and functions. Do not use dashes in the names, prefer underscore. The Python suggestion is to use lowercase names only.

Switch (Match)

From Python 3.10 there is a new match operator, but case ends do not need a break statement as they do not drop to the case below. Also, default is required.

Class __init___

Use __init__ instead of init for invoking name mangling, which will allow re-definition of the init function for inherited classes. Init in Python is a bit like a constructor. It does not have a return value, so you must use exceptions and tries to catch a problem.

Class member variables declared outside the init function are actually static and shared with all instances of the class. For instance variables, declare them inside init. VERY different from C++.

pip Installer

pip is a Python package installer. Note that the executable path of packages installed via pip are placed in /home/username/.local/bin by default.

Comments

Python doesn't truly have a multi-line comment feature, but you can use triple quotes """comment""" because it effectively creates an empty string.

Variable Function Args

In Python, *args and **kwargs are used to pass a variable number of arguments to a function. Example

def print_all(a, b, *args, c=10, **kwargs):

No other arguments should follow **kwargs.

Something like a C struct?

From Python 3.7 there is a new package that lets you use data classes, which are classes designed to just be data structures. See https://stackoverflow.com/questions/35988/c-like-structures-in-python

Lists

You can return a populated list from a function, but it requires use of the append() method.

# won't work, creates new local variable
def fill_list(mylist):
  mylist = [0,1,2]

#will work
def fill_list(mylist):
  mylist.append([0,1,2])  # or use .extend() function

Page last modified on April 16, 2025, at 06:41 PM