A photo of Evan Pratten
Evan Pratten

Hunting snakes with a shotgun

Python is a little too forgiving

A rather large number of people know me as “the guy who does weird things with python”. I would object to this title, but it is quite accurate. So, here are some of the things I like playing with in python. None of these are actually breaking the language, just little known facts and syntax. At some point I will share about actually breaking the language. For now, enjoy the weird things I have found over the past 6 years.

Type hints

A little known feature of python is called “type hinting” (PEP 484). This is actually quite common to see in standard libraries, and has it’s own special syntax:

# Here is a regular function
def meep(a, b):
    return a*b^2

# This function has no real reason to exsist, and is lacking any sort of documentation.
# Let's add a docstring to explain what it does

def meep(a, b):
    """ This function returns the result of a times b squared """
    return a*b^2

# Ok. The docstring explains the function, but is not too helpful 
# what are a and b? what does this return? 
# For all we know, a could actually be a string (in which case, this function would return a string)
# Let's fix that up with a type hint

def meep(a: int, b: int):
    """ This function returns the result of a times b squared """
    return a*b^2

# Thanks to the :int (called a type hint in case you didn't notice that yet), we now know that this function expects two ints.
# Now, to finish this up with a secondary type hint to specify the return type
def meep(a: int, b: int) -> int:
    """ This function returns the result of a times b squared """
    return a*b^2

# There. Now we can clearly see that this function takes too ints, and returns one int. 
# If only this was a requirement in the language. So many headaches could be solved.

Now, keep in mind that this is called a type hint. The python compiler (yes.. Give me a second for that one) does not actually care if you obey the hint or not. Feel free to send incorrect data into a hinted function and see what you can break. Critical functions should both hint and check the data types being provided.

Type declarations

Just like type hints for functions, python has hints for variables too.

# A regular variable. Must be declared with an initial value
my_state = None

# my_state is None, as it has not been set, but needs to exist.
# Let's assume that my_state is to be a state:
class State:
    status = False
    def toggle(self):
        self.status != self.status

# Finally, its time to set the state to something useful
my_state = State()
my_state.toggle()

# Ok.. I hate this. Let's start by using type declarations first
# Any variable can be un-initialized and just have a type. Like so:
my_state: State

# This works for anything
is_alive: bool
age: int
name: str

# Now, with this new knowledge, let's rewrite State
class State:
    status: bool
    def toggle(self: State) -> None:
        self.status != self.status

# And initialize my_state with slightly different syntax
my_state = State(status=True)

I have not found much use for this yet. Hopefully there is something cool to use it for.

One-line functions

This is more common knowlage. A function can be declared in one line

# Here is an adder function
def adder1(a:int, b:int) -> int:
    return a+b

# Here is a one-line adder function
adder2 = lambda a,b : a+b

# State from above can be compacted further:
class State:
    status: bool
    toggle = lambda self: self.status != self.status

Ternary operations

On the trend of one-line code, We have the one-line if/else, also known as a Ternary in more sensible languages.

# Here is an if/else
if 100 is 5:
    print("The world has ended")
else:
    print("All is good")

# Here is a smaller if/else
print("The world has ended" if 100 is 5 else "All is good")

Compiled python

This one is interesting. Python, like Java, is compiled into bytecode. So yes, it technically is a compiled language. To see said bytecode, take a look at any .pyc file sitting in your __pycache__

Blog formatting experiments

I am still playing with post formats, and various types of content. This is more random than I usually prefer. Let me know your thoughts on the social media platform of your choosing.