#๐Ÿ”’ how do i type hint accepting a generator

40 messages ยท Page 1 of 1 (latest)

unborn bear
#

i want to make an amoba i have 2 players and i want to make it easy to replace with a bot
i thought how generators but how do i hint that i need a generator i pass the board to and yields tuples of int, int?

carmine meadowBOT
#

@unborn bear

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

idle hill
#

So, generators are just iterators, basically

#

and you type hint an iterator with typing.Iterator[return_value]

#

so you'd want

from typing import Any, Iterator

def board_gen(board: Any) -> Iterator[tuple[int, int]]:
    ...
    yield (1, 1)
#

(apologies, I used parens rather than square brackets for the type hint - this is now fixed)

unborn bear
#

i need to hint accepting it

idle hill
#

I'm feeling dumb - sorry - is the parameter itself the generator?

unborn bear
idle hill
#

do you have the function you want to type hint?

#

that might be easier to look at

unborn bear
#

in the example i have to hint board_gen itself

idle hill
#

But I did type hint that

unborn bear
#

like Game(p1 = board_gen)

#

not like Game(p1 = board_gen())

idle hill
#

OK - so then that's a callable that passes in a board and returns an iterator

unborn bear
#

and ill need in the init to pass self.board to the p1

idle hill
#
from typing import Any, Iterator, Callable

class Board:
    pass

class Game:
    def __init__(self, p1):
        pass

def board_gen(board: Board) -> Iterator[tuple[int, int]]:
    ...
    yield (1, 1)

p1: Callable[[Board], Iterator[tuple[int, int]]] = board_gen
g = Game(p1 = board_gen)
unborn bear
#

whats the diff between typing.Generator and Iterator?

zinc siren
#

you really ought to use collections.abc.Generator; typing.Generator is deprecated

idle hill
#

The difference is I didn't know that existed - I just used the cheat sheets hints XD

#

but you can use typing.Generator as well

zinc siren
#

but Generator can receive values from yield, not just produce them

unborn bear
#

that may be what im looking for tho

zinc siren
#

most of the time this aspect of generators is not used

#

they're usually just used as fancy iterators

unborn bear
#

how do i code that accepting part?

zinc siren
#

assign the yielded value to a local variable

#

and then you'd have to use the send() method in the generator object

#

is this what you're really after though?

unborn bear
zinc siren
#

it's best if you didn't use generators until you're sure that's what you need

unborn bear
#

passing a snapshot aka .copy() would be best bc than they cant do illegal steps

#

and i need to define the players ssepearatly so i can do pvp bvp bvb anything

#

!close

carmine meadowBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.