Python is a dynamic, high-level 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, exceptions, had an extensive standard module library. Python can also be embedded in other ...

learn more… | top users | synonyms (1)

4
votes
3answers
377 views

Is it ok to have multiple classes in the same file in Python?

I'm freshly coming to the Python world after years of Java and PHP. While the language itself is pretty much straightforward, I'm struggling with some 'minor' issues that I can't wrap my head around — ...
40
votes
3answers
5k views

Experience of Python's “PEP-302 New Import Hooks” [closed]

I'm one of the developers of Ruby (CRuby). We are working on Ruby 2.0 release (planned to release 2012/Feb). Python has "PEP302: New Import Hooks" (2003): This PEP proposes to add a new set of ...
3
votes
1answer
92 views

How do I manage quickly changing python modules

I write Python code for scientific computation. As it is research I face among other two problems: the demands are quickly changing results need to stay reproducable Imagine you have a package A ...
11
votes
2answers
1k views

Object oriented vs vector based programming

I am torn between object oriented and vector based design. I love the abilities, structure and safety that objects give to the whole architecture. But at the same time, speed is very important to me, ...
1
vote
2answers
123 views

python - differences between reusable code vs. code for solving specific tasks

Reusable code (ex. libraries and frameworks) and code written to solve a specific task and not meant to be reused as a general tool (for example, code being used only by my 6 person team in a private ...
-1
votes
1answer
614 views

Is pywebsocket a production tool? [closed]

Is it a good idea to use pywebsocket in a production environment, since it is so widely used, but their google developer page states "pywebsocket is intended for testing or experimental purposes". ...
4
votes
1answer
151 views

Using Python's Method Resolution Order for Dependency Injection - is this bad?

I watched Raymond Hettinger's Pycon talk "Super Considered Super" and learned a little bit about Python's MRO (Method Resolution Order) which linearises a classes "parent" classes in a deterministic ...
6
votes
1answer
111 views

Python: Control subprocess from calling thread

I am working on a database interface to a MongoDB using Python Eve as an API. The database stores several documents containing parameters and values needed for some calculations. I wrote a Python ...
63
votes
8answers
14k views

Defensive Programming vs Exception Handling?

I'm working through the book "Head First Python" (it's my language to learn this year) and I got to a section where they argue about two code techniques: Defensive coding vs Exception handling. Here ...
5
votes
1answer
182 views

What is the most efficient way to implement a RESTful client app?

I am working on a web application for ranking some products based on some factors. The user send a search query to 5 restful webservices which by their turn send a response in a JSON format and ...
-1
votes
3answers
134 views

Python — Class and Object [closed]

I got a question that quite disturbs me a lot and I think it might help a lot if I had an answer to it. So I got this: class Klasse1: variable1 = "haha" class Klasse2: variable2 = "hoho" ...
1
vote
1answer
68 views

How to choose python module license as python itself

I would like to publish a python module and would like to publish it as the same license of python Below is a example of how Perl module does: This library is free software; you can redistribute ...
3
votes
1answer
106 views

How to move Python doctest examples into another unit test framework?

Years ago AFAIK the mathematitician and software developer Tim Peters discovered that very often the documentation of APIs tends to get out of date over time during the software live cycle, because ...
8
votes
6answers
8k views

Execute code every hour [closed]

I need to create a web service that executes every hour. It will be used to review data in a database and add alerts to a table in the same database if certain conditions are met/not met. What we ...
4
votes
2answers
1k views

Python classes with only one instance: When to create a (single) class instance and when to work with the class instead?

Given a Python class which will be instantiated only once, i.e. there will be only one object of the class. I was wondering in which cases it makes sense to create a single class instance instead of ...
9
votes
1answer
222 views

Python's join seems to focus not on the items to join, but on the symbol, as compared to Ruby or Smalltalk, for a design reason?

I thought one of the cornerstone of OOP is that, we have objects, which are the items we are interested in dealing with, and then we send messages to them. So it may seem natural that, I have a ...
4
votes
1answer
70 views

Signature-changing decorator: properly documenting additional argument

Let's say I have a custom decorator, and I want it to properly handle docstring of decorated function. Problem is: my decorator adds an argument. from functools import wraps def ...
0
votes
1answer
144 views

Docstring convention for Python __str__, __unicode__, and __repr__ class methods

What is the docstring convention in Python for the following "magic" class methods: __str__ __unicode__ __repr__ Should I add docstrings for these? If yes, what should they say (for each)?
0
votes
2answers
224 views

Does Python have any features which can be used for encapsulating private data?

Usually in OOP world we are told that modularity is a good practice and keeping loose coupling between module is a great thing. Encapsulation helps us achieve this loose coupling. In Java ...
0
votes
1answer
115 views

Parsing conditional statements

I've written a small utility in Python3 to help me copy my music collection from my NAS to a mobile device. The usefulness of this is that it will auto-convert flac files to ogg-vorbis (to save space) ...
9
votes
0answers
133 views

Unit testing for data munging pipelines made up of one-line functions

Reading Mary Rose Cook's Practical Introduction to Functional Programming, she give as an example of an anti-pattern def format_bands(bands): for band in bands: band['country'] = 'Canada' ...
-4
votes
2answers
139 views

Am i looking at HTML / Django the wrong way? [closed]

So I'm comfortable programming in Python, I love the minimalist nature of the language. However, I haven't been exposed to any Django yet. I do know html, css etc for web design but when making ...
4
votes
4answers
1k views

Why is %s better than + for concatenation?

I understand that we should use %s to concatenate a string rather than + in Python. I could do any of: hello = "hello" world = "world" print hello + " " + world print "%s %s" % (hello, world) print ...
1
vote
3answers
142 views

How To Extend Parent Methods in Children Classes?

There is a parent class with a method which many children use but many children extend the method, what is the best way to extend it without violating DRY? Here are my 2 current solutions: 1: The ...
0
votes
1answer
72 views

How to periodically serialise object's state and deserialise on initialisation in Python?

We're designing an application that is supposed to monitor a system for certain events, email interested parties to prompt them to take action when relevant and parse their replies. The application ...
2
votes
2answers
267 views

Is it OK to use (WTF) forms to validate against stuff from DB?

Form classes are intended (IMO) for submitted data against rules. For example: are passwords equal, is end date later than start date. submitted data--->|Form| Is it okay for Form classes to ...
1
vote
3answers
253 views

Function Overloading in Python

My book says that function overloading is not possible in python but it asks me questions like: WAP to find out the volume of a cube,cuboid and cylinder using function overloading. Do I have to ...
-2
votes
1answer
140 views

Can I use Qt open-source for my web-scraping website? [closed]

I apologize if I should be understanding this more readily but I'm a little new to this and dont understand the LGPL license. Here is the FAQ for it: http://www.qt.io/qt-licensing-terms/ I am making ...
3
votes
2answers
183 views

Where to put very specialised functions which heavily use one class?

I have a class which is centred around lower-level methods, to make this class much more useful it would be great to put some middle or higher level methods (i.e. methods which make a series of calls ...
0
votes
1answer
129 views

How to document **kwargs in python? [closed]

I have a function which has a large number of arguments. I want to have the names of the arguments available in the help() function, but I want the results as a dict. At the moment, I have the ...
1
vote
1answer
45 views

Architecture/technology for implementing Management console [closed]

I'm having a distributed environment which include several machines under the same network , which are being controlled by another computer in another network which is also running a UI for doing the ...
3
votes
1answer
113 views

A condition on an argument used in multiple calls of a method: enforced by caller or by the method?

Let's assume I have an object a of a class A. It has a method that needs an argument in form of another object of a particular type - but the argument should also be in particular state, because the ...
4
votes
3answers
586 views

Why do you need “self.” in Python to refer to instance variables?

I have been programming into a number of languages like Java, Ruby, Haskell and Python. I have to switch between many languages per day due to different projects I work on. Now, the issue is I often ...
1
vote
0answers
131 views

Is the logic behind `Asyncio.wait()` and async/await, the same, just the code is written differently (syntax)?

I'm learning Python, more specially parallel programming using Python Parallel Programming Cookbook by Giancarlo Zaccone. At the time the book was published async/await was still in the beta version ...
14
votes
4answers
2k views

Why do some programmers categorize C, Python, C++ differently? - regarding level

I am taking an introductory course on python and the instructor says that python is a high level language and C and C++ are low level languages. It's just confusing. I thought that C, C++, Python, ...
3
votes
2answers
231 views

Naming self or this something else, a good idea?

I'm finding that my code becomes more readable when I make a local reference to this/self, and/or call it something that makes sense in the context. "self" or "this" doesn't really indicate what kind ...
5
votes
1answer
96 views

Enclosing main invocation of a function in an if

As a part of my school CS class, I am reading Python Programming by John Zelle. In the book, Zelle talks about surrounding the invocation of a main method with if <condition>: main() ...
117
votes
15answers
18k views

Is it always a best practice to write a function for anything that needs to repeat twice?

Myself, I can't wait to write a function when I need to do something more than twice. But when it comes to things that only appear twice, it's a bit more tricky. For code that needs more than two ...
4
votes
2answers
137 views

Sort a list while putting together or after?

I have to read through an extremely large amount of network data from various log files, and compile relevant information about that data to perform statistical analysis on it (the top communicators, ...
6
votes
2answers
798 views

Bad practice to define constants based on a function call?

In Python when writing file management scripts I will often have a base path that is a constant, BASE = "C:/" Of course I'll be using that base path to create other paths later on, including other ...
0
votes
0answers
127 views

Why use Python's asyncio with coroutines to implement a server

I use Python's asyncio library to create a server that can handle telnet requests. Because asyncio's server loop is single threaded, I thought it would make more sense that when asyncio tells me that ...
0
votes
1answer
119 views

What i and n stand for in python? [duplicate]

I've been writing a program to pick random words from my list. However, to do that I had to imitate some solutions on the internet, and I succeeded. Unfortunately, there is something that I can't ...
0
votes
1answer
350 views

Python - Cache function and decorator

I am playing with cache functions using decorators. First, I use a generic function def memorize(func): cache = {} print "printing cache" print cache print "cache printed" ...
28
votes
3answers
9k views

Should I pass in filenames to be opened, or open files?

Suppose I have a function that does things with a text file - for example reads from it and removes the word 'a'. I could either pass it a filename and handle the opening/closing in the function, or ...
2
votes
1answer
88 views

Why is Python sorted() result like this?

I might be missing something obvious here, but why does Python 2.x sorted() function work like this? In [95]: l = [1, '5', 4, 2, '3', 'b', 'c', '-a'] In [96]: sorted(l) Out[96]: [1, 2, 4, '-a', '3', ...
25
votes
5answers
10k views

Is programming in Python faster than in C, C++ or Java? [closed]

There's a widespread belief among programmers that the more dynamic and loosely typed the language, the more productive the programmer will be in it. Guido van Rossum wrote about programming ...
2
votes
1answer
89 views

How does the Python runtime know which object a method is bound to?

When defining methods in a class we do have an argument for the object on which the method is invoked i.e self. Say I have a class Foo. class Foo: def m(self): pass now when we have an ...
16
votes
6answers
5k views

Best practices in comment writing and documentation

Commenting nowadays is easier than ever. In Java, there are some nice techniques for linking comments to classes, and Java IDEs are good at making comment shells for you. Languages like Clojure even ...
0
votes
1answer
53 views

Are there any pitfalls when replacing a Python module using sys.modules?

In a current project of mine, I have decided to not put any significant amount of code in __init__.py files, simply because I don't like them. In my head, an __init__.py file is just there to inform ...
2
votes
1answer
176 views

Best practices for a daemon that forks and becomes user [closed]

I want to write a small daemon in python under Linux that accepts connections and authenticates the user (details for this don't matter) and then forks a child for the connection and becomes the user. ...