Can someone please explain kwargs?

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
Rewritten Ennui
Veteran
Posts: 279
Joined: Thu Jul 14, 2011 1:50 pm
Organization: TwinTurtle Games
Contact:

Can someone please explain kwargs?

#1 Post by Rewritten Ennui »

I keep re-reading the FAQs and tutorials and stuff, but I still don't understand what **kwargs do. Someone please explain them to me in non-technical English :(

Edit: Why do I always miss a word when typing? D:
Last edited by Rewritten Ennui on Sat Sep 10, 2011 12:43 pm, edited 1 time in total.
I've swapped accounts to CheeryMoya, so this account is no longer in use. Refer to the new account if you want to contact me.

Twinturtle Games Website

User avatar
Aleema
Lemma-Class Veteran
Posts: 2677
Joined: Fri May 23, 2008 2:11 pm
Organization: happyB
Tumblr: happybackwards
Contact:

Re: Can someone please explain kwargs?

#2 Post by Aleema »

Hmm, as best as I can interpret (don't know what it is officially), it just means "put anything else here". Say you have a definition like this:

Code: Select all

def show_text(time,**kwargs):
    ui.text(time,**kwargs) 
When you called show_text, you'd put things you'd want passed on to ui.text after the variable, like this:

Code: Select all

$ show_text("forty",xpos=3)
Which would mean the function would alter the ui.text to essentially be: ui.text("forty",xpos=3)

I think.

bink
Regular
Posts: 49
Joined: Sat Jul 09, 2011 4:34 pm
Contact:

Re: Can someone please explain kwargs?

#3 Post by bink »

[quote="Aleema"]Hmm, as best as I can interpret (don't know what it is officially), it just means "put anything else here"./quote]

That's pretty much it. *args stands for a list of arguments, **kwargs stands for keyword arguments.
So you could do this:

Code: Select all

def testFunction(some_arg,**kwargs):
    print some_arg
    for key in kwargs:
        print kwargs[key]

testFunction("foo",a="bar",b="foo")
The result would be

Code: Select all

foo
bar
foo
You can use this to make the arguments optional. Ren'Py does this to pass arguments for styles through multiple levels of classes.

For the average Ren'Py user, this mostly means that you can put stuff in there like position and style arguments to customize the look of things.

Rewritten Ennui
Veteran
Posts: 279
Joined: Thu Jul 14, 2011 1:50 pm
Organization: TwinTurtle Games
Contact:

Re: Can someone please explain kwargs?

#4 Post by Rewritten Ennui »

So can it be used to insert different text and images? My head is incapable of comprehending :(
I've swapped accounts to CheeryMoya, so this account is no longer in use. Refer to the new account if you want to contact me.

Twinturtle Games Website

User avatar
Aleema
Lemma-Class Veteran
Posts: 2677
Joined: Fri May 23, 2008 2:11 pm
Organization: happyB
Tumblr: happybackwards
Contact:

Re: Can someone please explain kwargs?

#5 Post by Aleema »

Erm, not really? You don't need to worry about it, because it's for optional styling (styling, as in, changing the position, size, color, of things), and if you don't have something that needs to be flexible like that then you probably won't even use it. It's useful for something like Tooltips where you want the tooltip to display in a place as determined instance-by-instance.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Ocelot