* A memorandum written by a Python beginner with delusions because I understood a little about the future possibilities of Python 3.6 type annotation. </ font>
Python 3.6 was released at the end of the year when the Advent calendar was about to reach its finale.
-Example of news of python3.6 -Those who have confirmed the functions of 3.6 in advance
The Python 3.6 release from a Windows perspective could be summarized as follows:
--String literals have become more convenient, and type descriptions (annotations) have been expanded. -** Even in Windows environment ** The default character code is now UTF-8. ――Windows OS has nothing to do with Python, so it's okay to use it experimentally without thinking about anything.
The default UTF-8 conversion is large. In short, it will be comparable to Linux / Mac, so people in the 2-byte area will be less likely to suffer from extra stress around the character code.
Download and install the installer from the Official Site.
If you feel a little uneasy because it has just been released, you can install it by uncommenting "Install launcher for all users" for temporary users (..?)
After installation, type python in the console to enter the python3.6 interactive environment:
f "My name is {name}"
The new function of python3.6 "Formatted string literal (PEP-498)" can be used without any problem.
--To learn about new features in python3.6, including formatted string literals You should read the following series of blogs. http://atsuoishimoto.hatenablog.com/entry/2016/12/25/122220
In the following, we will briefly discuss type annotations that enable typed descriptions in Python.
In the dynamic language python, it is not necessary to specify the type in the variable definition.
Example ①:
person.py
class Person :
def __init__(self):
self.name = ''
self.age = 0
def __init__(self,name ,age):
self.name = name
self.age = age
def greet(self):
print(f"Hi, I'm {self.name} .")
print(f"I am {self.age} years old.")
If you try it in an interactive environment:
The variable age can be of type int or str.
In Python3.6, variable typing can be done widely.
Add a little to example (1) and specify the type for the instance variable of the class.
Example ②:
person.py
class Person :
#Add type annotation
name : str
age : int
def __init__(self):
self.name = ''
self.age = 0
def __init__(self,name ,age):
self.name = name
self.age = age
def greet(self):
print(f"Hi, I'm {self.name} .")
print(f"I am {self.age} years old.")
Execution result:
As a result of astonishment, some people may feel uncomfortable that the execution result of Q Taro does not change (although it is possible to describe the type in the argument of the method of Person class, but this Even if the result does not change). The description that specifies the type in Python3.6 is called (static) type annotation and is used for type checking in large-scale development etc. ("Class name.annotations" as shown in the last line of the execution result. (Used like this). It is different from the type specification in compilation languages such as Java and C #.
--For mypy for practical use of Python type annotations, read the following translated document.
http://qiita.com/t2y/items/2a1310608da7b5c4860b
Using Python for calling computational libraries etc. I wouldn't use type annotations for the time being (it doesn't seem to benefit much). On the contrary, in the team that operates many web services etc. with python, type checking using type annotation will be performed.
--Reference: I will use it on CI servers, etc., article http://qiita.com/k-saka/items/8f05c89f675af219e081
As I learned from the introduction to Python, in addition to various versions of Cpython (2.x series, 3.y series) that have various implementations in Python, starting with pypy, implementation systems on Jython, Ironpython, DartVM, etc. And so on ... Of course, there are plenty of tools in the library just for historical languages. There are many libraries that are combined with C / C ++ languages. Well-known libraries are good, but there is no guarantee that they will work with your implementation when you want to use a library made by some amazing person. I think that such a thing is to read the source and apply the patch by yourself ... I think that it was the open source culture of python so far, but it seems a bit tough with python which became major due to the recent big data & AI boom. ..
I don't understand mypy very much, so I can't write it as a concrete story, but I think there will soon be a great tool for using Python type annotations to interface with other languages. Or (and expect).
Recently, I mentioned python several times in Advent Calendar of New Language nim. I was wondering if there are many great tools that combine python and C / C ++, but I think we can also make a tool that combines nim and python that are compiled into C / C ++. if you can, In a form that takes advantage of the features of the compilation language nim, which has a python-like grammar.
The first is a tool that automatically rewrites a model defined in python into nim source code.
Actually, the above code example ② is a rewrite of the structure usage example displayed as "grab" on the top page of nim's official website to python (since python does not have a structure, I wrote it in class .. A simpler python-like description would be possible).
person.nim
type Person = object
name: string
age: int
proc greet(p: Person) =
echo "Hi, I'm ", p.name, "."
echo "I am ", p.age, " years old."
let p = Person(name:"Jon", age:18)
p.greet() # or greet(p)
Talks like coffeescript-> javascript are commonplace in recent years, and it seems quite likely to automate these conversions themselves, so I'd like to challenge after getting a little more familiar with both languages. And from here onward, the key to ease of use will be the automatic generation of interfaces that include mutual type checking and null safety.
With the recent trend of openness (?), Microsoft seems to be becoming a python-friendly company (python is supported in visual studio). With iOS / Android becoming widespread Is it because of the sense of crisis that the survival of the company will be jeopardized if it becomes the C # one-legged method?
It was xamarin, which was born from the .NET compatible environment mono derived from the Linux culture, that saved Microsoft. The story that C # users can develop on iOS / Android in addition to Windows is very appealing. mono is a CLR environment (C # execution environment) written in C language below. In Xamarin, the code is also written in objective-C / C ++ for iOS / Android support. Xamarin is a "C-language brothers". xamarinがより広く使われ始めると、もっともメジャーなスクリプト言語のひとつpythonを使いたい、C系のライブラリを呼びだしたいとの要求が広まるものと考えられる。ということで、.NET/mono/xamarin界隈では,近いうちにironpythonの話題が登場するのではないかと考える。その時、言語と実行系の組み合わせだけで複雑系になるpython周りの開発現場で型アノテーションを用いた静的解析が大規模に活用されるのではないか(...されるとまずいことになりそうな...)。
--Reference: New reader of IronPython https://www.infoq.com/jp/news/2016/08/IronPython-Leadership ironpython
I changed the punch line of python material to xamarin forcibly as a personal memory.
Xamarin has been open sourced since it was acquired by Microsoft, and in the last six months or so, a lot of information has been released, so it feels like it's finally time to use it. In fact, there are people who want to use such xamarin with python, right? ... So, as soon as I wanted to take a python (and sometimes nim) gaze around xamarin, which might save Microsoft in the future :)
Recommended Posts