Last newbie question

I have this code thanks to ndc86430

import typing

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

And I was wondering how to call g()

You really, really, really need to stop writing code you donā€™t understand and do some tutorials. In the long run, it will save you so much time and effort.

To call the function g, you write:

g(some_function, some_integer)

where some_function is a function that accepts an integer as argument, and some_integer is an integer.

Are you using mypy? If you are not using mypy, all those typing annotations are pointless.

If you donā€™t know what mypy is, then you are probably not using it.

3 Likes

See, your specifying functions and integers in your post. Donā€™t do that. It makes for wasted time and, before I saw through this trick, a lot of effort.

I donā€™t need mypy Iā€™m using my own output. Please stop spam my posts with none sense.

Iā€™m glad you donā€™t see me as a troll/help vampire but the answers you have given me have not helped this stigma.

I was going to comment on your last thread, but Iā€™ll do so here.

It looks like you have two large areas that you donā€™t understand too well: the mathematics and separately, the programming language. Iā€™d suggest you make things easier for yourself, by choosing one thing to focus on and coming back to the other later.

1 Like

The only thing I need now is the function call

I was actually programming this so it can explain how it works (using plain English, not mypy), and what itā€™s intents are and to prove thatā€¦

So saying I donā€™t understand the math is true but null. Thatā€™s the point of writing it in a program.

The function g takes two arguments: one is a function, f that itself takes an int and returns a bool and the second is an int.

Can you define a function that has the right shape (that is, takes an int and returns a bool)? Do you know how to pass a function to another? I assume you know how to pass an int to a function.

But in general, I do agree with @steven.daprano above.

No I canā€™t. I donā€™t know what values to pass.

You donā€™t know how to write a function that has the right form, or you donā€™t know which function(s) to pass? The former is a pretty basic thing so if youā€™re struggling there then as above, you need to work through some tutorials on the language. As for the latter, isnā€™t knowing what functions you want to do this on part of whatever this project is? I donā€™t think anyone here is really able to tell you what to do in this regard because they donā€™t really understand the project.

I donā€™t know what the parameters of g() are

The function g takes two arguments: one is a function, f that itself takes an int and returns a bool and the second is an int.

makes me think

g(int, int)

Is the correct way to call it

No. The first parameter is a function that takes an int and returns a bool. The second argument is an int, not a function. Your example call is passing the int function to g for both arguments.

Hereā€™s a rather trivial function that fits:

def f(x: int) -> bool:
        return True

and then I can call g, for example with

g(f, 3)

Can you replace ā€œ3ā€ with ā€œintā€

No, because the function f takes an integer as its argument, not a function. So that wouldnā€™t make any sense. I donā€™t know why youā€™re thinking that 3 and the function int are the same type of thing - they arenā€™t.

How would you make them the same (as int of course)

You donā€™t. Theyā€™re different things.

Weā€™re going round a bit in circles here. You donā€™t have a good enough grip on the language to tackle the problem youā€™re working on, so again, please spend some time doing that.

Sure, Iā€™ll do that

1 Like

Itā€™s probably worth saying again, but I donā€™t understand your project - Iā€™m not a mathematician. I took my definition for g directly from what you said in your other thread, which Iā€™ll quote here:

# 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.

So yes, f is a function int -> bool and n an integer. As you can see, I donā€™t have anything that restricts n to be non-negative, but I suppose you could do that yourself if you wanted to.

Iā€™m reading about default values as parameters to functions right now.

Iā€™m not sure if itā€™s exactly what Iā€™m looking for but it sure is a cool read

Default parameters havenā€™t come up in this thread and I hope youā€™re not getting confused again between those and type hints - I recall that happened in another one of your threads (Calling Functions using Default Values).

But as @steven.daprano suggested, you need to start with the basics.

I have a few projects Iā€™ve been neglecting it is true

Iā€™m mostly just trying to do a worthwhile job though, the rest I would do myselfā€¦

Yeah I was getting them confused lol

You can still get to that page? I thought they had a habit of tearing down nonsense threadsā€¦

Could you get me something to read? My Google is failing.

ā€œcalling type hint functionsā€ comes up with nothing