This article is participating in Python Theme MonthActive link
The opening statement
My active section focuses on the App side. Learning Python is all about personal automation (funny). My few Python articles are about simple crawlers that don’t require cookies, login, or authentication.
So I don’t really have much to say about Python technically, but I’ve learned and thought about Python from writing crawlers and prior iOS programming.
My overall coding style is front-end oriented and Swift.
This article you can think of as someone standing outside the door, looking at a bit of Python thinking.
The other thing I want to say is that everyone has their own coding style, and although Python has the Python specification, I do follow my own style. Do not spray if you do not like it.
Note: the code in this article is based on Python3.7, and the IDE for writing the examples is VSCode.
Define a variable assignment
You don’t need to declare the type of the variable, and you don’t need any modifiers to modify the variable. You’re done:
a = 1
Copy the code
If you want to change the value of A, you can simply change it:
A = "Hello, Python" a = TrueCopy the code
A variable, can save different data types.
Python is a weak language, and weak languages have the advantage of being flexible, so that it is possible to have a very long logical implementation of a function, assign a variable multiple times, and then not know what type the variable is.
It is possible to declare the type of a variable in Python, but I rarely see this:
- Writing in Python
a: str = "aa"
b: int = 1
Copy the code
The IDE will also indicate the type of the variable when it is used:
So some of them have a bit of Swifty:
- The writing of Swift
Var a: String = "aa" var b: String = "aa" var b: String = "aaCopy the code
How’s it going? Is it on the inside?
However, it is important to note that even if the type of a variable is declared in Python, the type of the variable is not fixed, and you can still take whatever type you want.
a: str = "aa"
a = 2
print(a)
Copy the code
Even though a is defined as STR, it is possible to assign a value of 2 to a without error, and the call to print is fine.
I think that while it is convenient for variables to be of any type, it is best to stick to the type assigned to the variable in the first place for reasons of safety or other people’s reading habits. Reduce type judgments, although isinstance (a,int) is also handy.
Alias the type
We can alias existing or custom types to make writing and understanding easier.
For example, if we want to define the coordinates of a position by x and y, we can do this:
- Writing in Python
Location = (float, float)
Copy the code
Location represents a tuple containing two variables of type float.
- The writing of Swift
typealias Location = (Float, Float)
Copy the code
You may find this useful, but you may find it useful in the next section.
The input and output parameters of a function are defined
Here’s a requirement: there’s a function that needs the ability to input two coordinates and find the distance between two points.
Why is that so hard, you say? Do you know how to write Python?
def distanceBetweenPoint1(point, toPoint):
x1, y1 = toPoint
x2, y2 = point
dx = x1 - x2
dy = y1 - y2
distance = math.sqrt(dx * dx + dy * dy)
return distance
Copy the code
Call:
distance1 = distanceBetweenPoint1((0, 2), (5, 7))
Copy the code
Point, toPoint, toPoint, point, toPoint, point, toPoint, point, toPoint
You might say: I’ll just write a comment that tells the user what type of input parameter this function has, or even write a small example.
Yes, you can write a comment and it’s good practice to write a comment, but if we can make the input parameter type explicit and semantic, we can write less comment and play around more.
Here’s what I did:
Location = (float, float)
def distanceBetweenPoint2(point: Location, toPoint: Location):
x1, y1 = toPoint
x2, y2 = point
dx = x1 - x2
dy = y1 - y2
distance = math.sqrt(dx * dx + dy * dy)
return distance
Copy the code
Call:
distance2 = distanceBetweenPoint2(point=(0, 2), toPoint=(5, 7))
print(distance2)
Copy the code
Even if you don’t write a comment, what does the function mean?
But as you can see, this function does not specify a return value type, and we may not even know that this function has a return value.
So, here is the final expression of this function:
Location = (float, float)
Distance = float
def distanceBetweenPoint3(point: Location, toPoint: Location) -> Distance:
x1, y1 = toPoint
x2, y2 = point
dx = x1 - x2
dy = y1 - y2
distance = math.sqrt(dx * dx + dy * dy)
return distance
Copy the code
Call:
distance3 = distanceBetweenPoint3(point=(0, 2), toPoint=(5, 7))
print(distance3)
Copy the code
I use two aliases Location and Distance to express the Location and Distance respectively. I define the types in and out of the function parameters.
And when you call this function, you get a clear indication of the type, which is both clear and safe:
Distancepoinenpoint3 (point=”haha”, toPoint=”hehe”). I put both tuples in the STR type passed in. The IDE does not tell you something is wrong. It is worth noting that exceptions are not thrown until the code is run.
As for how to write Swift, I will write a simple point here, the concrete implementation will not write:
typealias Location = (Float, Float) typealias Distance = Float func distanceBetweenPoint(point: Location, toPoint: Distance {/// the logic is similar to Python's implementation}Copy the code
conclusion
This article is all about making Python variable types more explicit. Much of this article focuses on how to ensure type security through better coding styles. If you are a Python veteran, you may find my code ridiculous, redundant, and weird.
However, from my development perspective, the determination of variable types is a reassuring thing. I happen to have two programming languages in iOS development, Objective-C and Swift, which are weak languages and strong languages respectively, which is the painful realization of the loss.
Weak languages push the explicit type of a variable to runtime, and the price of flexibility is that you must always be aware of what type the data is!
Strong languages specify data types at compile time, trading flexibility for security.
No one is good, no one is bad, existence is reasonable.
Given JavaScript’s popularity for so many years, why does Microsoft still make TypeScript?