New answers tagged

0

This is cleaner for the example you give: def sizeC(self, arg=None): if arg: self._size = arg return self._size However, Robert Harvey's right in that this is not the way to do fluent interfaces.


0

Can you expand on what you mean by dynamically add properties? I take it that you need to do this while code is running, rather than simply edit the source files? If so maybe check out named tuples as part of the Python collections module https://docs.python.org/2/library/collections.html You can create a class dynamically like this EmployeeRecord = ...


0

What I did was use self.__dict__.update(json) which copies items in the JSON dict into the object's dict and so they appear as properties of the object. The client can then use dot notation to access or mutate them.


1

The concept underlying your question is so important I feel it needs another answer rather than just a comment (as I had started to do). The other 3 answers thus far provide some useful points of consideration on whether a given situation merits using what you call "nested function calls". But perhaps a more important point is hidden in the comments under ...


8

This really depends on how much nesting you use. After all, you are allowed to use function results directly in expressions to improve readability. Both, code that does not use nested expressions (like assembler code), and code that uses too much nested expressions is hard to read. Good code tries to strike a balance in between the extremes. So lets look at ...


1

It is absolutely not a bad practice in general. Functions call accept values and one way of producing a value is by calling another function. When I see a variable being defined, like: parsed_value = cashParser(input) ... I have to consider that parsed_value might be used more than once and I'll probably have to check if this is true or not (what if my ...


4

I think whether it's good or bad depends a lot on context. The main reason it might be considered bad is that it (arguably) makes the code harder to read and debug. This is especially true when you are first learning to program. As your coding skills (and code) gets more advanced, there are times when this is acceptable. For example, consider an error ...


-2

... maybe because it's not that difficult to write one yourself def flatten(l): return flatten(l[0]) + (flatten(l[1:]) if len(l) > 1 else []) if type(l) is list else [l] ... and then flatten all you want :) >>> flatten([1,[2,3],4]) [1, 2, 3, 4] >>> flatten([1, [2, 3], 4, [5, [6, {'name': 'some_name', 'age':30}, 7]], [8, 9, [10, [11, ...


9

This is not a Python vs Other Languages distinction - it's actually Value Types vs Reference Types distinction. Python uses reference types, and while many modern languages also tend to use reference types, it's common to compare Python(or any language, actually) to C/C++, which use value types. (I'm simplifying things a lot here - there are languages that ...



Top 50 recent answers are included