Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options answers only not deleted user 55400

Python is a dynamically typed, high-level interpreted programming language. Its design focuses on clear syntax, an intuitive approach to object-oriented programming, and making the right way to do things obvious. Python supports modules and exceptions, and has an extensive standard module library. Python is general-purpose and thus used widely, from the web to embedded systems.

6 votes
Accepted

Decorators in Python

Python decorators are nothing more than a callable themselves (a function, or a class instance with a __call__ method). …
Martijn Pieters's user avatar
4 votes
Accepted

Python recipe question: Use of **kwds instead of specific named argument in itertools recipe

In Python 2, you cannot put a keyword argument after *args, that's a syntax error: >>> import sys >>> sys.version_info sys.version_info(major=2, minor=7, micro=8, releaselevel='final', serial=0) >>> def … the positional catch-all, then it captures the first positional argument: >>> def random_product(repeat=1, *args): return repeat, args ... >>> random_product('foo', 'bar') ('foo', ('bar',)) In Python
Martijn Pieters's user avatar
5 votes

Write two versions of class with two different super classes without violating DRY?

In this specific case, you don't need to subclass defaultdict at all, because defaultdict is not much more than a dict subclass with an added __missing__ method. You can simply subclass LimitedDict a …
Martijn Pieters's user avatar
2 votes
Accepted

Python replace function for classes? Like namedtuple _replace?

namedtuple has such a method because it itself is immutable. Other immutable types in the standard library have one too, like datetime.datetime for example. It is not a common pattern to use with mut …
Martijn Pieters's user avatar
3 votes
Accepted

In python, do we have class encapsulation at the level of the object?

This has nothing to do with encapsulation, and everything with a Python implementation detail as to when string literals produce a string object. … What is specifically happening here is that Python uses constants to store literal values used in code. Your 'the juice bar' value is such a constant. …
Martijn Pieters's user avatar
2 votes
Accepted

Python shelve class rename

Shelve uses pickle to serialize objects. Pickle loads objects by name; the module and class name are stored and looked up when deserialising. The easiest way to support 'legacy' names is to simply cr …
Martijn Pieters's user avatar
11 votes

Is modifying an object's __dict__ to set its properties considered Pythonic?

In Python 3.3, a new type was added, types.SimpleNamespace(), and in the documentation it is described thus: The type is roughly equivalent to the following code: class SimpleNamespace: def __ … __name__, ", ".join(items)) Note the __init__ method of the type; you cannot get a better endorsement of the technique than the Python documentation. …
Martijn Pieters's user avatar
7 votes
Accepted

How to comment on a PEP?

Python PEPs are discussed on the Python-Dev mailinglist, which anyone is free to join. There is a Gmane gateway for web reading too. … the python@mail.python.org list), and the model has worked well enough for this community. …
Martijn Pieters's user avatar
10 votes

Usage of __ while declaring any variables or class member in python

The feature is specifically meant for preventing accidental clashes between a parent class member and subclasses. The name is mangled by prepending it with the class name; the atribute __foo on a cla …
Martijn Pieters's user avatar
273 votes
Accepted

'import module' vs. 'from module import function'

Importing the module doesn't waste anything; the module is always fully imported (into the sys.modules mapping), so whether you use import sys or from sys import argv makes no odds. The only differenc …
Martijn Pieters's user avatar
9 votes

How can I multiprocess my program?

The multiprocessing module on the other hand, is intended to make distributing work across multiple python processes as easy as using threads for the same kind of work. …
Martijn Pieters's user avatar
8 votes
Accepted

How safe is it to rely on thirdparty Python libs in a production product?

I've been deploying python web applications for years now, and this has rarely been a problem. … tool like buildout gives you another avenue to build a fully re-playable deployment configuration, and not only can you pin your library versions with it, you can instruct buildout to never install a python
Martijn Pieters's user avatar
3 votes
Accepted

How should I distinguish between built-in types in Python?

You definitely can use duck typing here, although I'm not sure why you want to accept both a dictionary and a string in this case. I'd ask for forgiveness instead of permission in this case: try: …
Martijn Pieters's user avatar
2 votes

python random with a skew

Sure, that's quite simple to do. Just take the sum of both dominance values and take a random.randrange() value of these. If that value is lower than the dominance value of parent A, you picked that …
Martijn Pieters's user avatar
15 votes

Are there any design patterns that are unnecessary in dynamic languages like Python?

Mostly, Singletons in python are replaced with a module. Modules in python are by their very nature Singletons; the python interpreter creates these once and once only. … All other patterns in Design Patters I've used in Python at one time or another, and you'll find examples of them throughout the Python standard library and in Python itself. …
Martijn Pieters's user avatar

1
2 3 4 5
15 30 50 per page