Hot answers tagged

60

Because the standard writers don't want to actually assert an implementation. They want to define what it does, but not necessarily how it does it. So, for example, if you look at the GNU C++ version of find_if, you will see that the implementation is slightly different from what you give, which is based on the C++ standard: template<typename ...


5

In general, you should not use except:, except for a few extremely rare circumstances, most notably log-and-reraise, or at the very top-level after which the program must end. Do not use it in libraries, or any non-top-level method, at all. Instead, please use except Exception:. It catches most 'normal' errors, except a few special ones (KeyboardInterupt, ...


4

Just rethrow the exception when you get it: try: #do stuff except KeyboardInterrupt: raise except: #do other stuff You specify that you get an exception that you know that you want to handle differently than default case, but you don't know how to handle it - so you just pass it, by throwing it again. Some working example: #!/usr/bin/python2 if ...


3

You've chosen an unusual architecture. What you are calling a stack machine isn't really what we typically call a stack machine. A stack machine is a type of computer architecture. Since your stack machines cannot execute a program, they aren't an instance of computer architecture, and thus what you are implementing is not properly a stack machine. But, ...


3

You want to make sure all expressions satisfy a condition. The Python builtin all is made precisely for that. Since we have no need to introduce a new variable name, here I'll use the name _, which is a valid Python name and is the convention for a "throw-away" variable. all(_ is None for _ in (x,y,z))


3

For important software, it is common to support a release for multiple years. With Python, support is mostly in the form of bugfixes. You can see the changelog for the whole list of fixes between versions (see 3.5 full changelog). Between versions such as 3.5 and 3.4, not only bugs were fixed but also new features were added. E.g. 3.5 added the @ matrix ...


2

I think what you're looking for here is a Sequence Diagram. These allow you to visualize the order in which various modules call eachother via the use of arrows. Constructing one is simple: Draw your starting class with a dotted line below it. Draw the next class/method in the call trace with a dotted line below that Connect the lines with an arrow, ...


2

Language designers generally try to avoid adding new keywords, especially for language features added after the language has already gained some popularity. Every keyword they add is an identifier that programs aren't allowed to use for other purposes, so adding a keyword could potentially break existing programs. Language designers have to weigh the ...


2

It's ideal for scripting something quickly because of its expressive capability and wide variety of support libraries. Dynamic type checking and lack of explicit variable declaration make it a poor choice for large projects involving hundreds of thousands, or even millions of lines of code. The same thing goes for mission critical or safety critical ...


2

Quoting https://www.python.org/dev/peps/pep-0008/#package-and-module-names: Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged. And for classes: Class names should normally ...


1

Yes, this is possible and quite common for packages that serve as both command-line tools and imported libraries. In setup.py, add a module's function as an entry point: setuptools.setup( ... entry_points={'console_scripts': [ 'foo = my_package.some_module:main_func', ]}, ... ) To create a script named foo that calls the my_func ...


1

The same reason we receive updates to Windows 7 after the release of Windows 8, 8.1, 10.… The same reason you can take your car to the dealer for a service and new tyres, even though it's ten years old. It would be a bit rubbish if you had to buy a whole new car every time you broke a tail light. The same reason you can build a new porch at the ...


1

This is a little hard to reason about in isolation but I generally like the second approach better. It gives you the flexibility to pass any three objects (including your mocks). If it is not a class invariant that they all be constructed with the same parameters, this seems to be a good thing. The classmethod you provide for the case that you want A, B, C ...


1

There is no global solution that just sets the problem variable to NULL and continuous on with normal program flow. This appears to be the only way: try: variable=values[5] except: variable='error' You'll have to change the original one liner to a 4 liner everywhere you use a list variable. It is the most appropriate way since it allows specific ...



Only top voted, non community-wiki answers of a minimum length are eligible