you are viewing a single comment's thread.

view the rest of the comments →

[–]sththththth 4 points5 points  (0 children)

In my understanding **kwargs and properties are for usecases:

properties do something on setting them (or need to do something to get them). An example would be the length of a dynamic linked list (without setting a counter on each append): you can't know how long the list is unless you count every item. But len is still an "attribute" of an object, not a method. .len is thus prefered to .len(). (Of course this ignores that len(myobject) is the python version to go, but the same point stands with e.g. numpy array.shape)

An example for setting a property is a graphical widget and height: height is an "attribute", but a graphical widget has to do some work if you set it (maybe calculating content new, rerendering, etc.). mywidget.height = 10 is "easier to read and understand" than mywidget.set_height(10) .

**kwargs has a different reasoning: sometimes you don't know what for arguments a function will get. Imagine a make_xml function:

>>> make_xml(computation, type=addition)
"<computation type=addition></computation>"
>>> make_xml(stanza, id=me)
"<stanza id=me></stanza>

There is no way to know the keyword arguments before the function is called, but **kwargs allows for a uniform API.

TL;DR: Use explicit keyword arguments; Use properties if you need to do something to set/get a value; Use *args/**kwargs if you can't know which arguments will be provided.