Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i(*args) statement
#1
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)
Reply
#2
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/38617...e-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.
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to compare two parameters in a function that has *args? Milan 4 1,181 Mar-26-2023, 07:43 PM
Last Post: Milan
  *args implementation and clarification about tuple status amjass12 10 3,912 Jul-07-2021, 10:29 AM
Last Post: amjass12
  [SOLVED] Good way to handle input args? Winfried 2 2,003 May-18-2021, 07:33 PM
Last Post: Winfried
  Two Questions, *args and //= beginner721 8 3,421 Feb-01-2021, 09:11 AM
Last Post: buran
  does yield support variable args? Skaperen 0 1,640 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  is there a way: repeat key word args Skaperen 2 2,199 Feb-03-2020, 06:03 PM
Last Post: Skaperen
  Using function *args to multiply multiple arguments allusernametaken 8 5,947 Nov-20-2019, 12:01 AM
Last Post: allusernametaken
  Passing string args to Popen CardBoy 3 4,195 Jan-16-2018, 09:22 AM
Last Post: Gribouillis
  Why args type is always tuple, when passed it as argument to the function. praveena 5 5,252 Jan-16-2018, 09:07 AM
Last Post: praveena
  subprocess with args haye 0 4,396 Oct-23-2017, 10:54 AM
Last Post: haye

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020