Things about Python

by: burt rosenberg
at: university of miami
date: jan 2021

Quick Facts

Here are some quick facts to help you get started in Python. Things that distinguish Python for other languages, that you need to know.
Indentation Matters
Python is a block structured language, such as Java or C. A blocks is a sequence of statements that is bound to run as a unit. Most languages use punctuation such as curly braces to mark the block, others use keywords. Python marks them by indentation. This is a very rare occurrence where whitespace is structurally important. Perhaps the only other common example is the use of the tab in Makefiles.

To complicate things, spaces and tabs are not interchangeable when it comes to Python indentation. If possible set your editor to "show invisibles" so that spaces and tabs are distinguishable. Use your editor's beautification tools to enforce tabbing consistency. Use a simple rule, such as one tab per block level. Beware cutting and pasting of code, as that might not preserve tabs.

Dear Codeblock:
The controlling element of a code block ends with a colon. This includes class, def, for, if, elif, and else. Many find this redundant, since the block is indented. But if the block contains only a single statement, it can follow the colon on the same line. But nobody does this and neither should you.
It talks to its Self
The methods of an object run in the context of the instance of the object. In all Object Oriented languages there is some means by which the method obtains a reference to the object to accomplish instance dependent accesses.

Python provides the reference by "stuffing" it into the define's parameters, as an automatically provided first argument. By convention, all programmers call this variable self.

For a method f of an object referenced by r, with let's say one parameter, the call is made,

	r.f(a)
and that will match the method definition,
	def f(self,a):   # self <- r, when called as above.
where the value of r is copied into self.

Methods in Python, hence, always have one more parameter in their definition than in their calling. (Typically, it is also possible to call method f with on parametre a, of an object referenced by r, where the object is of class R, by R.f(r,a). )

Some things can't be changed
Python supports immutable objects, as does Java, and simulated by C in the const storage class. However, the existence of immutables just seems to be more prominent when programming in Python.

A string is immutable. To modify it is to create a new string that includes the modification. Lists and Dictionaries are mutable, however their neighboring data structure, the Pair is not. For the beginner, it is enough just to realize that some popular data structures cannot be updated.

Immutablility is very important for a full object oriented program. In such programming languages, even an integer constant, say 1, is an object. If it were mutable, the value of 1 could be changed to 2, for instance. Even in a functional language, like C, mutability has to be be considered. If a string "hello world!" is passed to a function for later use, and some bit of code that has a reference to that string replaces the last six characters in that string, "hello world!" could become "hello gremlin". In a C type language, this is only avoided by strick programmer hygiene. In Java or Python it just cannot happen.

Just Iterate It
The For construct in Python triggers iteration of an iterator. And when it comes to for-type loops, that's what you got.

Many data structures are naturally iterators. A string should and does present one by one its characters, in their order, when subject to iteration. Lists, their elements. Python creates a range that represents an integer sequence, and the For construct iterates over that.

Modern languages often share this iteration mania, but Python seems to take it one better by removing old stye C-like for's, to rely entirely on iterators.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

author: burton rosenberg
created: 25 jan 2021
update: 25 jan 2021