py3minepi / py3minepi-legacy

Original attempt to port Minecraft Pi to Python 3
https://github.com/py3minepi/py3minepi
Other
25 stars 7 forks source link

API: Python already provides both 'flat' and 'vector' interfaces #21

Open jonathanfine opened 9 years ago

jonathanfine commented 9 years ago

We can have Python provide both 'flat' and 'vector' interfaces, using a single code-base.

Let world denote the Minecraft world we are connected to. Python translates

world[1, 2, 3]

into

world.__getitem__((1, 2, 3))

Now write

vec = (1, 2, 3)

Python translates

world[pos]

to exactly the same as before, namely

world.__getitem__((1, 2, 3))

What's more, Python translates

world[pos1:pos2]

into

world.__getitem__(slice(pos1, pos2))

where the slice has start and stop attributes pos1 and pos2. From this we can have __getitem__ do what we think is the right thing.

We can even write, if there is need for it

world[pos1, pos2:pos3]
doismellburning commented 9 years ago

Can you please clarify the issue here?

jonathanfine commented 9 years ago

Some may think we have to choose between a vector or flat interface. I'm noting here that we do not. I'm also noting that this capability is build into Python's language API for container[...].

In short we have a real opportunity here to be Pythonic, and doing so would provide many benefits.