Python Forum
i(*args) statement - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: i(*args) statement (/thread-1915.html)



i(*args) statement - chelovek - Feb-04-2017

Hi,

I'm studying code for a Kivy app and am having trouble understanding a piece of syntax. For context, this is part of an app that lets a user drop a file into the app and then populates the file-name in the interface.

What does the for loop with the "i(*args)" statement do, exactly? I would understand if it were drops[i], but have no clue what i(*args) does.

Thanks. Happy to provide more details if that helps. Even a point in the right direction (i.e. a topic to look up) would be helpful.

class WeatherApp(App):
    def build(self):
        self.drops = []
        Window.bind(on_dropfile=self.handledrops)
        return DropFile()
    def handledrops(self, *args):
       for i in self.drops:
            i(*args)



RE: i(*args) statement - buran - Feb-04-2017

This is not full code as it's not clear what (will) be in the self.drop. It looks it comes from here:
http://stackoverflow.com/questions/38617194/kivy-on-dropfile-multiple-bindings

TestApp.drops will hold DropLabel.on_dropfile methods., so when a file is droped on TestApp window this will file _on_filedrop event which will trigger handledrops method of the TestApp which will get as arguments reference to the window object and as secon argument - the full path and name of the droped file. handledrops will loop over the drops list and will pass this arguments (star unpacked) to all objects in the drops list. in the SO these are on_filedrop methods.


RE: i(*args) statement - ichabod801 - Feb-04-2017

In general, *args allows a function to accept a variable number of positional parameters, and allows passing of a sequence as different positional parameters:

def show(*args):
    print(args[1])
    return [args[0]] + args[2:]
def add(a, b):
    return a + b
x = [2, 3]
add(*x)
There is also **kwargs, which allows similar functionality for dictionaries and variable sets of keyword parameters.

*args and **kwargs are often used for passing large variable lists through a function or method. The function or method at hand may not care about them, but a function or method it calls will care about them.