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()
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.
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.
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
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