Difference between revisions of "Python"

From Hawk Wiki
Jump to: navigation, search
(Created page with "## Python 3.6 type checking make sure python is 3.6 or above <pre> python3 -m pip install -U mypy</pre> Example code with typecheck <pre class="brush:python"> from typing imp...")
 
Line 16: Line 16:
 
</pre>
 
</pre>
  
then just run
+
To test typing, just run
 
<pre>
 
<pre>
 
mypy testtype.p
 
mypy testtype.p
<pre>
+
</pre>

Revision as of 23:55, 20 November 2018

    1. Python 3.6 type checking

make sure python is 3.6 or above

 python3 -m pip install -U mypy

Example code with typecheck

from typing import List
Vector = List[float]

def scale(scalar: float, vector: Vector) -> Vector:
    return [scalar * num for num in vector]
	
# typechecks; a list of floats qualifies as a Vector.
new_vector = scale(2.0, [1.0, -4.2, 5.4])
print(new_vector)

To test typing, just run

mypy testtype.p