Python development _ function annotations

Source: Internet
Author: User

When reading python APIs, I found an interesting thing: Function Annotation (Function Annotation) of python)

Original article:

4.7.7. Function Annotations
Function annotations are completely optional, arbitrary metadata information about user-defined functions. Neither Python itself nor the standard library use function annotations  any way; this section just shows the syntax. Third-party projects are free to use function annotations  documentation, type checking,  the  attribute of the function as a dictionary  have no effect on any other part of the function. Parameter annotations are defined by a colon after the parameter name, followed by an expression evaluating to the value of the annotation. Return annotations are defined by a literal ->, followed by an expression, between the parameter list  the colon denoting the end of the  statement. The following example has a positional argument, a keyword argument,  the >>>  f(ham: 42, eggs: int = ) -> (, f.(>>> f(: < >, : , : 42

After a brief look, I did not understand the meaning of the parameters. I wrote the program again:

  f(ham: 42, eggs: int = ) ->      (, f.     (  f()

Running effect:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32,    >>> ================================ RESTART ================================>>>: < >, : 42, : >>> 

The running effect is the same as that described in the python API.

After searching some data, we found the parameter meanings:

Let's take a look at several demos:


   f(ham: 42 = , eggs: int = ) ->      (, f.     (  f()

Running effect:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32,    >>> ================================ RESTART ================================>>>: < >, : 42, : >>> 

   f(ham:  = , eggs: int = ) ->      (, f.     (  f()

Running effect:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32,    >>> ================================ RESTART ================================>>>: < >, : , : >>> 

   f(ham:  = , eggs: str = ) ->      (, f.     (  f()

Running effect:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32,    >>> ================================ RESTART ================================>>>: < >, : , : >>> 

         (str ==  || str.equals(""  flag =  ( i = 0; i < str.length(); i++ ((str.charAt(i) < 'a' || str.charAt(i) > 'z'&& (str.charAt(i) < 'A' || str.charAt(i) > 'Z'= 

Here you probably know what I want to say!

Summary:

 f(ham: 42, eggs: int = ) -> (, f.( f(ham: int = 42, eggs: str = ) -> (, f.(

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.