Ok, so here is a question that is likely trivial but I am not readily finding any info on it.
I have a bit of code that I want to make into a function but the code is expecting a 'float' type variable.
How do I specify in the function definition to only accept floats when this function is called? Or is that not pythonic?
Thanks,
Brian H.
being pythonic ?
Re: being pythonic ?
You need to use Python's "Type Annotations".
https://docs.python.org/3/library/typing.html
MicroPython supports this for function arguments of built-in types like `float` etc, but will not actually verify the types at runtime. The support mostly exists so that the parser doesn't fail on existing Python 3 code, and also the viper emitter uses it for optimisation.
Re: being pythonic ?
You don't usually specify the types of a function's input arguments in Python. As a dynamic language, functions and operators in Python generally adapt to the type of their inputs as far as possible and that's what other programmers would expect your function to do too.
What happens if you pass an int instead of a float? I would try and make sure the function works as expected in this case.
What happens if you pass an input that doesn't make sense, like a string or some sort of handle like a function or an iterator? If the resulting behaviour doesn't make it clear to the user what was wrong, consider adding some error checking of your own so the user sees a meaningful exception.
Type hints are there to help with static code analysis when Python is used for larger projects, so that you can detect when a function is being used incorrectly by automatically checking the source code rather than having to run the code and see the exception. As the doc jimmo linked to says, they're not actually enforced by the Python interpreter itself.
What happens if you pass an int instead of a float? I would try and make sure the function works as expected in this case.
What happens if you pass an input that doesn't make sense, like a string or some sort of handle like a function or an iterator? If the resulting behaviour doesn't make it clear to the user what was wrong, consider adding some error checking of your own so the user sees a meaningful exception.
Type hints are there to help with static code analysis when Python is used for larger projects, so that you can detect when a function is being used incorrectly by automatically checking the source code rather than having to run the code and see the exception. As the doc jimmo linked to says, they're not actually enforced by the Python interpreter itself.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: being pythonic ?
You can enforce checking (although this isn't usual) as follows:
But this would fail with an int - it would probably be worth testing for int and casting to a float.
Code: Select all
def foo(arg):
if not isinstance(arg, float):
barf()
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: being pythonic ?
Thanks all.
Cheers,
Brian H.
Yes, I see the wisdom. I had thought I'd have different functions for other data types, but now lean towards data type detection and steering execution appropriately within the one function to handle the incoming data type.functions and operators in Python generally adapt to the type of their inputs
Cheers,
Brian H.