1
#!/usr/bin/env python


class Countries(object):
        def __init__(self,*_args,**_kwargs):
                self.name = 'japan'
                self.continent = 'asia'
                self.capital = 'tokyo'
                print _args
                print _kwargs

        def display(self):
                print self.name, self.continent,self.capital




iraq = Countries('iraq','bagdad','asia')

iraq.display()

india = Countries('india','delhi','asia')

india.display()

I have written a self teaching example above.I know that irrespective of the parameters I am always assigning

'japan' , 'asia' , 'tokyo'

to the member variables of the class.I wrote the above in an attempt to understand

   *_args and **_kwargs

The output of the above program is as below

('iraq', 'bagdad', 'asia')
{}
japan asia tokyo
('india', 'delhi', 'asia')
{}
japan asia tokyo

where _args is ('iraq', 'bagdad', 'asia')

and __kwargs is {}

why is a null string being printed for _kwargs and all arguments printed as a list for _args

liv2hak
  • 14,472
  • 53
  • 157
  • 270
  • 1
    `{}` isn't a "null string" by any reasonable meaning of the term… – abarnert Apr 02 '13 at 21:53
  • 1
    Also, given the way you've called this, what did you _expect_ to see in `_args` and `_kwargs`? – abarnert Apr 02 '13 at 21:56
  • @abarnert - good question.I thought it was similar to argc and argv[].Obviously _kwargs is used for keyword arguments for e.g capital='bagdad'. What I don't get however is the basic reason for such an approach? Is it to support variable number of arguments for constructors? – liv2hak Apr 02 '13 at 21:57
  • 1
    Makes sense. But if you think about it, the only reason you need `argc` at all is that C arrays don't know their own length. (That's why Python has a `sys.argv` but no `sys.argc`.) Anyway, the basic reason for `*args` and `**kwargs` is to support both variable arguments and keyword arguments, and for _all_ functions, not just constructors. Almost any prototype you could describe to a person, you can easily implement in Python. (There are a few minor limitations in 2.x, but they're fixed in 3.x.) – abarnert Apr 02 '13 at 22:14
  • 1
    And as for why you _want_ keyword arguments: Look at something like [`subprocess.check_output`](http://docs.python.org/2/library/subprocess.html#subprocess.check_output) in the stdlib. With keywords, I can do `check_output(args, universal_newlines=True)`. Without them, I'd have to do `check_output(args, None, None, False, True)`—which means I'd have to look up what all the other arguments are, and their default values, just to say I don't care. Plus, it's a whole lot less readable, unless I add a comment for each argument to show which parameter it matches (and which are non-default). – abarnert Apr 02 '13 at 22:17
  • @abarnert - excellent explanation.I am a C/C++ programmer just getting started on Python.This is the kind of explanation that I need at this stage :) thanks. – liv2hak Apr 02 '13 at 22:31

1 Answers1

4

Because you aren't passing in any keyword arguments. If you did Countries('Iraq', capital='Bagdad'), then it would print something for kwargs.

Silas Ray
  • 25,682
  • 5
  • 48
  • 63
  • 2
    I would also suggest looking at this answer to understand the mechanics behind *args and **kwargs: http://stackoverflow.com/a/3394898/288570 – DrakeAnderson Apr 02 '13 at 21:56