Advanced Python (3.12) Part 1
I have been programming in python for quite some time, without ever having learned it from scratch. This is why I often come across built in functionality that I am not familiar with. The following is a small list of interesting python functions to improve your code quality.
Iterator
An iterator is an object that has the __next__ and the __iter__ function. The first returns the“next” object (in whichever sense we define), and the second an instance of the object itself.
Funny Thing: the “for loop” just needs an Iterator object, so we can create unusual things like this (which doesn’t make any sense, but it shows well how iterators works):
import random
class Iterator:
state = 0 #static variable
#The next element is just two more then the current state
def __next__(self):
self.state = self.state + 2
#Say there is no next element when somethign random happens
if random.random() > 0.8:
raise StopIteration()
return self.state
def __iter__(self):
return selfit = Iterator()
for i in it:
print(i)The last prints as expected some even increasing numbers. If we run the “for loop” again, we get values that seamlessly join where we left off, because we didn’t reset the “state” variable. We can also manually call “next(it)” to either get the next value, or an exception.
Iterators are important in order to only compute values when we really need them.
Get Bernhard Eisvogel’s stories in your inbox
Join Medium for free to get updates from this writer.
(Iterables are very different to Iterators, because they are “collections of objects” that can return you an Iterator)
map()
m = map(lambda x: x**2, [0,1,2,3,4])It is important to note that m is an iterator, not a list. This means that after running this line, none of the values have been computed, it has just been created an iterable object.
We can also use “m” in a “for loop”, as explained in the last section. If we run print(m), we get something like:
<map object at 0x1106869d0>
In particular, m is not an array but an object with specific functionality.
@classmethod and @staticmethod
class Date:
formatName = "Weekday"
# Gives some info about the day
def __init__(self, day="Monday"):
self.day = day
def info(self):
print("Today is", self.day)
# Return the format of the current Class
@classmethod
def getFormat(cls):
return cls.formatName
@staticmethod
# Returns if two gives dates are equal
def equal(a, b):
return a.day == b.day
# Subclass for a more precise date
class PreciseDate(Date):
formatName = "Weekday + day of month"
def __init__(self, day="Tuesday", date=1):
super().__init__(day)
self.date = dateThe first class “Date” has one kind of all (two! ;D) functions decorators that are always available in Python. @staticmethod declares, as expectable, a function as static.
On the other hand, @classmethod is a function that also takes the current class as an argument, which becomes interesting (and cleaner), if we want to write a function that changes depending on the current subclass, but is still defined in the superclass. I invite you to look at the example code for clarification.







