Python Forum
Python 3 Function Annotations
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3 Function Annotations
#1
is there any example for function annotations. function annotations not working while running a code. is there any way to make it.
Reply
#2
How are they not working?
Reply
#3
(Feb-28-2017, 10:35 AM)stranac Wrote: How are they not working?
i have restrict to int but it was allow string. its not checking while running brother. please suggest me some examples.
Reply
#4
Annotations aren't meant to restrict anything.
In fact, python doesn't do anything at all with annotations, their interpretation is left for third parties.
If you want type checking, take a look at mypy
Reply
#5
Or you type check yourself:
def increment(a):
    if not isinstance(a, int):
        raise ValueError('Can only increment int.')
    return a + 1
Another strategy is to force the int:
def increment(a):
    return int(a) + 1
This will accept strings, but turn them into ints if possible. If it's not possible, python will handle raising the error for you.

Either of these strategies requires checking for exceptions further up in the program with try/except blocks. In general, you should only be using these strategies when handling input you don't control. If you control the input, you need to make sure the input is right.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(Mar-01-2017, 06:01 AM)charlesprince Wrote:
(Feb-28-2017, 10:35 AM)stranac Wrote: How are they not working?
i have restrict to int but it was allow string. its not checking while running brother. please suggest me some examples.
Thank you

(Mar-01-2017, 06:21 AM)stranac Wrote: Annotations aren't meant to restrict anything. In fact, python doesn't do anything at all with annotations, their interpretation is left for third parties. If you want type checking, take a look at mypy
I have been looking for type checking like JAVA. why it's not available in python or else is there any way to make it. please let me know
Reply
#7
(Mar-13-2017, 03:09 PM)charlesprince Wrote: why it's not available in python or else is there any way to make it. please let me know

Python follows a different philosophy. If it has the attributes/methods of the required object, you let it work. That's what allows the sum function to handle integers, strings, and lists. You can type check if you want, but you have to write it yourself. See my previous post.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
In Python 3.6 is added a syntax for this.

https://www.python.org/dev/peps/pep-0526/
https://docs.python.org/3/library/typing.html
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
Python is a way to get away from Java (and other) type checking.
It's not done because a variable can hold any type, and it can change when you need it to
If you think about it for a while, that is an extremely powerful asset!
Reply
#10
(Mar-13-2017, 03:09 PM)charlesprince Wrote:
(Mar-01-2017, 06:01 AM)charlesprince Wrote:
(Feb-28-2017, 10:35 AM)stranac Wrote: How are they not working?
i have restrict to int but it was allow string. its not checking while running brother. please suggest me some examples.
Thank you
(Mar-01-2017, 06:21 AM)stranac Wrote: Annotations aren't meant to restrict anything. In fact, python doesn't do anything at all with annotations, their interpretation is left for third parties. If you want type checking, take a look at mypy
I have been looking for type checking like JAVA. why it's not available in python or else is there any way to make it. please let me know
I thought, It may reduce few codes, thats it. Thanks
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020