Reputation: 970
i tried help(**kwargs), and help(kwargs) but received an error.
There are online sites that show you all available methods for a dictionary, but none for something not so newbie friendly like kwargs.
I am asking for a generic method to find built in python info so that I don't have to ask those questions online. Thanks.
Edit_1, for example, I saw this from someone else's code:
def somefunction(**kwargs):
for key, value in kwargs.item():
print key, value
How do I find out more methods like the above example, item() ?
Upvotes: 0
Views: 97
Reputation: 16940
kwargs is just a name and ** is use to unpack dictionaries (kew words argument )
NB: kwargs could be any name you like
Little bit about kwargs:
>>> def foo(**new_kwargs):
... print new_kwargs
...
>>> dic = { 'key' : 'value' }
>>> foo(**dic)
{'key': 'value'}
>>>
>>> foo(key='value')
{'key': 'value'}
>>>
>>> foo(dic)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() takes exactly 0 arguments (1 given)
>>>
And :
If you want to see all the attributes and method for a particular object (even class is an object in python): Use dir built-in function
:
dir(...)
dir([object]) -> list of strings
And if you want to see any particular attributes is method or function, you can use callable
to check if they are callable:
>>> dir(dict)
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__',
'__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__',
'__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items',
'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault',
'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
>>>
>>> callable(dict.keys)
True
>>>
>>> class foo(object):
... class_variable = 'Some String'
... def function():
... return 'Some String'
...
>>> dir(foo)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'__weakref__', 'class_variable', 'function']
>>>
>>> callable(foo.class_variable)
False
>>> callable(foo.function)
True
>>>
What ever you show extra in dir(foo) are from base class object
.
Upvotes: 0
Reputation: 174698
*kwargs
just means that a method takes multiple keyword arguments that aren't defined.
Take this example:
def foo(a,b,c=10,**kwargs):
pass
This method has two required positional arguments, one argument c
that is optional (since it has a default value), and then takes any number of additional arguments. All these are valid calls:
foo(1,2,hello='world',bar='baz')
foo(1,2,3,hello='world',bar='baz',zoo='foo')
Since they are variable, there is nowhere to list them.
The best you can do is use the built-in help()
function and hope that the programmer has written some docstrings
to highlight the use of the function you are interested in:
def foo(a,b,c=10,**kwargs):
'''This function takes extra arguments
that can be used to calculate the
meaning of life. The first two arguments
a and b are required, c is set to 10 as
a default.
Examples:
foo(1,2,hal='are you there?')'''
Now, when you do help(foo)
it will print out that string.
dir()
is another built-in that is useful. It returns a list of attributes for the passed in object, or a list of names (variables) that are available in the current scope.
If you want to know what all things you can do with an object (basically, what goes after the .
), type dir(theobject)
. For example, dir({})
will list all the attributes of a dictionary.
Upvotes: 2
Reputation: 4318
If you are looking for documentation of builtin module,this link might help: http://effbot.org/librarybook/builtin.htm. Your question is not specific, it would be great if you add more details.
Upvotes: 0