1) I see in code that people will define subclasses like this:
def __init__(self, **config):
server = config.get('server', 'dnd')
what does the ** mean? I've also seen it as one asterisk. Or is that different?
2) i have also seen people define methods of classes with one underscore in front of the name and this is somehow special. why?
I know these may be basic questions, but I've never learned python formally and need to get on these.
Thanks to anyone who can help?
1) I see in code that people will define subclasses like this:
def __init__(self, **config):
server = config.get('server', 'dnd')
what does the ** mean? I've also seen it as one asterisk. Or is that different?
A parameter starting with ** means the function/method accepts named parameters that will be available as a dictionary:
>>> def f(**keywords):
... for key in keywords:
... print key, keywords[key]
...
>>> f(var1=1, var2=3)
var1 1
var2 3
When the parameter starts with a single * it means positional arguments are accepted:
>>> def f(*a_tuple):
... for item in a_tuple:
... print item
...
>>> f(1,7,'car')
1
7
car
2) i have also seen people define methods of classes with one underscore in front of the name and this is somehow special. why?
It is a warning to other programmers that it is a private method and should not be used directly. It is not enforced by the interpreter.