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