being pythonic ?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
uxhamby
Posts: 34
Joined: Thu Nov 14, 2019 9:47 pm

being pythonic ?

Post by uxhamby » Mon Apr 20, 2020 9:48 pm

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.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: being pythonic ?

Post by jimmo » Tue Apr 21, 2020 2:59 am

uxhamby wrote:
Mon Apr 20, 2020 9:48 pm
How do I specify in the function definition to only accept floats when this function is called?
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.

nekomatic
Posts: 37
Joined: Thu May 08, 2014 9:31 pm

Re: being pythonic ?

Post by nekomatic » Tue Apr 21, 2020 9:51 am

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: being pythonic ?

Post by pythoncoder » Tue Apr 21, 2020 11:31 am

You can enforce checking (although this isn't usual) as follows:

Code: Select all

def foo(arg):
    if not isinstance(arg, float):
        barf()
But this would fail with an int - it would probably be worth testing for int and casting to a float.
Peter Hinch
Index to my micropython libraries.

uxhamby
Posts: 34
Joined: Thu Nov 14, 2019 9:47 pm

Re: being pythonic ?

Post by uxhamby » Tue Apr 21, 2020 9:11 pm

Thanks all.
functions and operators in Python generally adapt to the type of their inputs
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.

Cheers,

Brian H.

Post Reply