Typing (newb) arrows and colons

n: int -> bool
def f(n):
    int -> bool
   return f(n) >= f(n+1)

Can anyone read this. How do I fix it?

Where is this code from, @heatJack?

It has some very basic errors in it that you can understand better with a simpler example. It also looks like it is not meant to be run (the ‘->’ is document or textbook notation, not Python code statement), so where is it from?

The arrows from typing (import typing)

These arrows -> are being used for annotations (including type annotations) but not in the context you have them.

I am really wondering what you think should be the meaning of this line:

n: int -> bool

If you want to annotate a function to accept int and return bool, do it like this:

def f(n: int) -> bool:
    return f(n) >= f(n+1)

The arrow -> is being used to annotate the function (return value) because the colon : already has a different meaning in that context.

def f(n):
    # What you write after the colon of the def statement is already
    # the first statement of the function's body.

Also to use type annotations you do not necessarily need to import typing.

1 Like

Thank you so much

def f(n: int) -> bool:
    return f(n) >= f(n+1)

Except that will give you RecursionError. Perhaps you mean this?

def f(n: int) -> bool:
    return n >= n+1

Can you help us understand what that code is, @heatJack ?

Where is the code from?

Is it copied from somewhere? Did you write it after studying something?

Thank you for explaining the ->, @heatJack & Václav. The Data Classes docs page mentions using the : for typing, which I’ve used and am familiar with [ def foo(bar: int): ]. I had to search for ‘Type Hint’ to find the -> reference.

This is what I’m working with

Plain Python has a lot of functionality for arithmetic (even more with NumPy) but it looks like you would rather need computer algebra (symbolic computation).

For this you should check: SymPy and possibly SageMath.

So far I have

def f(n: int) -> bool:
return f(n) >= f(n+1)

Which is correct, other than the fact that it is recursive… is there a version of this code that is non-recursive?

Here are the rules;

# We represent elements of N_∞ as functions f : int -> bool, where we only care about
# the values of those functions for nonnegative inputs n, and the functions are
# assumed to satisfy f(n) >= f(n+1) for all n >= 0.

The text you’ve posted there only tells you that the functions f have to satisfy the property that f(n) >= f(n + 1), not that they are defined recursively.

So the answer to your question is yes: you can define another function that checks the property, e.g.

import typing

def g(f: typing.Callable[[int], bool], n: int) -> bool:
    return f(n) >= f(n + 1)

and then define the functions f in whatever way makes sense for you.

I have no idea what you’re trying to do with this really and I see this going the same way as your other threads.


Thank you man!!!

This is the second time I actually got an answer that made sense to me I’m so happy!

This topic has been solved