Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 23-02-2009, 01:35 AM   #1 (permalink)
Apprentice
 
Join Date: Aug 2006
Posts: 62
Arrow Python: why it worked later, function keywords:


The following code didn't work:

class X(object):
def f(self, **kwds):
print kwds
try:
print kwds['i'] * 2
except KeyError:
print "unknown keyword argument"
self.g("string", kwds)

def g(self, s, **kwds):
print s
print kwds

if __name__ == "__main__":
x = X()
x.f(k = 2, j = 10)

However the following did:

class X(object):
def f(self, **kwds):
print kwds
try:
print kwds['i'] * 2
except KeyError:
print "unknown keyword argument"
self.g("string", **kwds)

def g(self, s, **kwds):
print s
print kwds

if __name__ == "__main__":
x = X()
x.f(k = 2, j = 10)

Please explain
ravi.xolve is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 23-02-2009, 08:48 AM   #2 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Python: why it worked later, function keywords:

Its because your g function is set to accept one formal parameter and optional keyword arguments after it. So its set to accept only 2 arguments inside the class, the class itself and s.

In Python, the * is usually used to "unpack" a tuple or a list for passing them as arguments to a function, while the ** is used over dictionaries to make them deliver keyword arguments over to other functions that accept it as the last parameter.

For more, there's Python documentation reference.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Only Linux distro that actually worked. :P Tech_Wiz Open Source 34 15-12-2007 08:03 PM
Piracy worked for us, Romania president tells Gates anandk Random News 29 07-02-2007 02:49 AM
has anyone worked in powerbuilder technology ? tech_cheetah QnA (read only) 1 24-01-2006 01:48 AM
I found a CALER ID CAPABLE MODEM it worked too but .. vijaythefool Networking 5 30-06-2005 09:04 AM
.:: Correct Way Of Specifying Keywords In Meta Tags? ::. tuXian Software Q&A 13 13-03-2005 06:35 PM

 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:08 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2