[SOLVED] Python Extended Iterable Unpacking

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Lord Hisu
Regular
Posts: 58
Joined: Sun Feb 11, 2018 2:31 am
Contact:

[SOLVED] Python Extended Iterable Unpacking

#1 Post by Lord Hisu »

I just answered a question that gave me more questions about how Ren'Py treats Python code.

The problem is really simple... we have a string, and want to unpack all the words in it using split.
Something like this:

Code: Select all

b = renpy.input("Type a string")
c, d = b.split(" ")
Now, if our string has one space, like "my string" everything should be fine. But lets say my string is "this is my string", and I want to get the first and last words in that string. In pure Python, this would do the job:

Code: Select all

b = renpy.input("Type a string")
c, *trash, d = b.split(" ")
This (*var) syntax tells the interpreter that it should pick the first variable returned, put it in c, then pick the last one, put it in d, and all the rest goes to the trash variable.

But when I do the same thing in Ren'Py using a python block, or even a $ sentence, it raises an error. "Parsing the script failed. Invalid syntax".
I would like to know if Ren'Py parses Python code somehow before the Python interpreter itself, or if it passes everything in the python block to the interpreter, and then handle potential errors.
I believe it is the first option, since this feature exists since Python 3.0, and if that's the case, I would like to know if it's possible to include that in the next update.

Sorry for all the trouble. :roll:
Last edited by Lord Hisu on Tue Feb 13, 2018 10:16 pm, edited 1 time in total.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1461
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Discord: xavimat
Contact:

Re: Python Extended Iterable Unpacking

#2 Post by xavimat »

Ren'Py doesn't use python 3 yet.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
Lord Hisu
Regular
Posts: 58
Joined: Sun Feb 11, 2018 2:31 am
Contact:

Re: Python Extended Iterable Unpacking

#3 Post by Lord Hisu »

Oooh, and I thought I was using Python 3 all along!
The print function tricked me, but I guess it's a simple __future__ import, right? :cry:

Thanks, xavimat.

Post Reply

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot]