1

I want to write program that check args. Do annotations are use for this purpose? actually what dose annotations do ?

def bar(x: str ) -> int:
    return 12.5

but I can put a int to x and return a float!! so what annotations do ?

>>> bar(12)
12.5
>>> 

I know for checking arg I can use :

style1:

def foo(x: str ):
if type(x)==str:
    return 0
else :return (-1)

to do this by annotations :

def foo(string : str) -> str:
    if foo.__annotations__.get('string')==type(string):
        print('continue...')

that mean annotations does not force args ?! so why do not use style1 ? actually I want to know that annotations do in simple terms ?

Community
  • 1
  • 1
Zero Days
  • 851
  • 1
  • 11
  • 22
  • 3
    They annotate? Possible duplicate of [What are good uses for Python3's "Function Annotations"](http://stackoverflow.com/questions/3038033/what-are-good-uses-for-python3s-function-annotations) or http://stackoverflow.com/questions/13784713/what-good-are-python-function-annotations?lq=1 – vaultah Aug 18 '15 at 07:00
  • Read the PEP: https://www.python.org/dev/peps/pep-3107/. Also useful is https://www.python.org/dev/peps/pep-0484/ – cdarke Aug 18 '15 at 07:05

0 Answers0