#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    11
    Rep Power
    0

    Question Python Pointers ?


    After my crash course in Python syntax I started learning PyQt, where I encountered something that wasn't covered in my crash course tutorial:

    Code:
    #
    # hello.py
    #
    
    import sys
    from qt import *
    
    class HelloButton(QPushButton):
        def __init__(self, *args):
            QPushButton.__init__(self, *args)
            self.setText("Hello World")
    
    class HelloWindow(QMainWindow):
        def __init__(self, *args):
            QMainWindow.__init__(self, *args)
            self.button = HelloButton(self)
            self.setCentralWidget(self.button)
    
    def main(args):
        app = QApplication(args)
        win = HelloWindow()
        win.show()
        app.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
        app.exec_loop()
        
    if __name__ == "__main__":
        main(sys.argv)
    In the following line:
    def __init__(self, *args):
    There is a * before args, and I dont' have a clue what that means
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    11
    Rep Power
    0

    Thumbs up Variable amount of arguments


    I figured it out

    Apparently, * before a function argument list means you can have a variable amount of arguments, for example:

    Code:
    def test(*args):
        print "\n".join(args)
    
    test("argument 1", "argument 2", "argument 3")
    will print:

    Code:
    argument 1
    argument 2
    argument 3
    (The arguments are collected in a tuple)
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Alexandria, VA
    Posts
    5
    Rep Power
    0
    There are two different ways of passing/receiving non-explicit arguments *args and **kw.

    *args gives you a tuple of the extra arguments passed to the function **kw gives you a dictionary of the extra keyword arguments to the function.

    BTW, the use of the names 'args' and 'kw' is just a convention, you can use any name you like. It's the leading astrick or two that tells the interpreter what to use them as.

    Check out this link on python.org for more info:

    http://www.python.org/doc/current/tu...00000000000000
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    11
    Rep Power
    0

    Talking It's like you read my mind


    After seeing the **kw form just a moment ago I came here and you conveniently already answered my question

    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo