python - What are good uses for Python3's "Function Annotations" -


Function Annotation:

I took part in a snippet of the display code of the function annotation of Python 3. The concept is simple, but I can not imagine why it was implemented in Python 3 or was used well for them. Maybe SEO can highlight me?

How it works:

  def foo (a: 'x', b: 5 + 6, c: list) - & gt; Maximum (2, 9): ... function body ...  

After an argument, everything after the colon is an annotation, and -> The function has an annotation for the return value.

foo.func_annotations will return a dictionary:

  {'a': 'x', 'b': 11, 'c': list, 'return': 9 }  

What is the importance of getting this available?

I think this is really great.

Coming from an academic background, I can tell you that annotation has proved itself priceless to enable smart static analyzer for languages ​​like Java. For example, you can define words such as access to state restrictions, threads, architecture limits etc., and there are some tools which can be read later on and to provide assurances to the compiler to give them the Can process. You can also write things that check pre-condition / postcondition.

I think that something like this is particularly necessary in Python due to its weak typing, but in fact there has not been any construction that became part of this straightforward and official syntax.

There are other uses for annotation beyond assurance. I can see how I can implement my Java-based tool Python. For example, I have a tool that lets you provide special warnings in the methods, and when you tell them that you should read their documents, you are prompted (for example, you have a method that is negative Should not be implemented by value, but not intuitive by name), with annotation, I could write technical for something like Python. Similarly, a device that organizes methods in a large class based on tags, if there is an official syntax.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -