Questions tagged [python]

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.

Filter by
Sorted by
Tagged with
9
votes
1answer
17k views

How Python random shuffle works?

How shuffle from random works in Python? I ask because it works very fast. When I try to write shuffle it works 1 minute for 10^6 element, but Python shuffle does that in 8 sec?
9
votes
10answers
99k views

How long would it take to learn Python? [closed]

I have decided to take the time out after work to learn Python. Python appeals to me because at work (Web and eLearning Company), I have to follow out very repetitive tasks like delete all these tags, ...
9
votes
4answers
382 views

What to cover in a “introduction to python” talk?

I'm in a student team that is focusing on web development. My teammates are interested in Python and I'm the only one that has learned it, so I was asked to give an "introduction to Python" talk next ...
9
votes
4answers
904 views

Class decorators in Python: practical use cases

I am looking for practical and non-synthetic use cases of Python class decorators. So far, the only case which made sense to me is registering a class in a publisher-subscriber system, e.g. plugins or ...
9
votes
1answer
573 views

Is using '{}' within format strings considered Pythonic?

I just learned you can write '{}{}'.format(string_a, string_b) instead of '{0}{1}'.format(string_a, string_b) in Python, i.e. you can omit the numerals for the string format parameters when you ...
9
votes
2answers
7k views

Machine Learning With Categorical and Continuous Data

This question could go here or on S.O. perhaps... Suppose that your training dataset contains both categorical and continuous data such as this setup: Animal, breed, sex, age, weight, ...
9
votes
1answer
8k views

Can one edit a built-in Python module?

I'm currently learning Python and I'm at the point in the book about using the Math library. I looked on the Python website and noticed the library was a bit scarce and am writing some more useful ...
9
votes
1answer
282 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 ...
9
votes
3answers
3k views

Recommendations for implicit versus explicit line joining

I would like to know recommendations about Implicit Line Joining versus Explicit Line Joining in Python. In particular, do you favor one form over the other? What do you recommend as the general ...
9
votes
2answers
2k views

Why are Python strings allocated on the stack?

According to this Python code visualizer, Python strings are allocated on the stack and not on the heap. Why is this? I thought they would be similar to Java where Strings are allocated on the heap.
9
votes
4answers
925 views

General rules for writing a X compiler to Z in Y

Suppose X is the input language, Z is the output language, then f is the compiler, which is written in language Y. f = X -> Z Since f is only a program, I think Y can be any language, right? So ...
9
votes
1answer
9k views

Why is flask CLI recommended over Flask.run?

In Flask 0.11 a flask CLI was introduced. Both the docs and the changelog state this is recommended. Development Server docs: Starting with Flask 0.11 there are multiple built-in ways to run a ...
9
votes
2answers
6k views

How should I name functions that return values in Python?

I'm confused about choosing names for my functions in Python. Sometimes Python built-in functions are imperative such as: print function and string method find. Sometimes they aren't such as: len its ...
9
votes
1answer
1k 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 ...
9
votes
4answers
8k views

why empty function are needed

I started learning python and I am wondering why empty function are needed in a programming language e.g. in python: def empty_func(): pass even in shell scripts empty function empty functions ...
9
votes
1answer
3k views

Good practices for packaging Python programs

I've been using Python for a while now, both in the context of personal and professional projects. One thing that occured to me recently is that I had never thought about a good way to deploy Python ...
9
votes
1answer
681 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' ...
8
votes
6answers
16k 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 ...
8
votes
5answers
3k views

Why is unit testing private methods considered as bad practice?

Context: I am currently working on a small project in Python. I commonly structure my classes with some public methods that are documented but mainly deal with the high level concepts (what a user of ...
8
votes
9answers
9k views

How to become an expert in Python, PHP and Javascript? [closed]

So I've been programming for about 9ish months now, and I've taught myself some Python, some PHP and some Javascript. I want to become better at these languages - I can hack something out, but a lot ...
8
votes
4answers
1k views

Why do textbooks use pseudocode rather than real languages?

In colleges and in algorithm textbooks, it is quite common for the teacher and author to explain control flow in pseudo-code. With the advent of more expressive languages like Python and Haskell among ...
8
votes
1answer
491 views

Refactoring a 1500 LOC method that only builds the graphical UI [closed]

I'm currently scratching my head over how to refactor a method that basically only builds the UI. The method is more than 1500 lines of code (LOC) long - and counting. It has grown, there was no plan ...
8
votes
4answers
576 views

Why isn't the ‘format’ method more widely used in Python?

I'm probably missing something here, after searching I couldn't find an answer. I've explored quite a few Python projects and one thing I keep noticing is the fact that the majority of them continue ...
8
votes
3answers
8k views

Lambda expressions with no parameters in Haskell and / or lambda calculus

In eager languages like Scheme and Python, you can use a lambda expression without parameters to delay evaluation, e.g. in Scheme (Chicken Scheme): #;1> (define (make-thunk x) (lambda () (+ x 1))) ...
8
votes
4answers
5k views

Is using nested function calls a bad thing?

In a recent homework assignment I ended up calling my functions in an ugly way uglyReceipt(cashParser(cashInput())) the program itself worked perfectly but I still felt like I was doing something ...
8
votes
4answers
3k views

When to import names into the global namespace? (using x::y, from x import y etc.)

I've been programming in various languages for about 10 years now. And I still haven't figured out when it is a good idea to import something into the global namespace (using x::y in C++, from x ...
8
votes
6answers
11k views

What language to use for prototyping and creating quick scripts? [closed]

Right now, I use Python for my quick scripts and prototypes (e.g. algorithms, my pseudocode is very Python-like as well). The oher languages that I am familiar with include Java, C, x86 Assembly and ...
8
votes
4answers
1k views

For Python programming and being Pythonic, why “never is often better than *right* now”? [closed]

In the Zen of Python, I can understand most parts of it, except: Now is better than never. Although never is often better than *right* now So I think doing it now or getting results now is better ...
8
votes
1answer
3k views

What is the historical reason why Python uses the double underscore for Class Private members

In python a module private function or variable is named _foo. If it is private to a class it is names __foo, The mechanics behind how these work are different, however that is of little relevance to ...
8
votes
3answers
2k views

how to programtically build a grid of interlocking but random sized squares

I want to create a two dimensional layout of rectangular shapes, a grid made up of random sized cubes. The cubed should fit together and have equal padding or margin (space between). Kind of like a ...
8
votes
4answers
4k views

Using Clojure instead of Python for scalability (multi-core) reasons, good idea? [closed]

After reading http://clojure.org/rationale and other performance comparisons between Clojure and many languages, I started to think that, apart from ease of use, I shouldn't be coding in Python ...
8
votes
7answers
3k views

What would be an appropriate algorithm to factorise numbers in the range of a few billion?

I'm learning Python at the moment and to give me reasons to apply what i'm learning I'm having a crack at some of the problems on Project Euler I'm currently on number 3, which is to determine the ...
8
votes
5answers
2k views

Python: What is the point of using “import”?

I am not very clear on this aspect. Let's say you have a bunch of .py files that are their own separate modules. Why does each .py file need to import the others when they use that class? Or do they? ...
8
votes
3answers
674 views

How can I start shipping code?

I have just started learning how to program using LPTHW, and as my skill level develops I would like to start shipping code whether or not it's ready to be shipped, just to get into the habit of ...
8
votes
1answer
9k views

When to use private methods in Python

I have a class, but every method in it should be private (apart form __init__ and __str__). Should I denote every method with a double underscore, or is that deemed bad practice?
8
votes
3answers
2k views

Space between negative sign and variable name

I tried doing a Google search, as well as searching this Stack Exchange site but could not find a question relating directly to this. The PEP 8 -- Style Guide for Python Code has lots of good style ...
8
votes
2answers
3k views

Is there a convention for returning multiple items?

In Python specifically (I don't know if this generalizes) is there a "best" way to return multiple items from a function? def func1(): return a,b #equivalent to (a,b) def func2(): return[a,...
8
votes
4answers
6k views

Pattern for requests with long response times?

We are currently maintaining a homegrown python "web server" where generating the response for some requests can take a very long time mostly due to heavy computations - these requests are basically ...
8
votes
4answers
21k views

How to clean up nested try/except/else?

When writing code, I often want to do something like this: try: foo() except FooError: handle_foo() else: try: bar() except BarError: handle_bar() else: ...
8
votes
2answers
864 views

Should I prefer Python generators to lists?

Python iterators can be very memory efficient. Should I always prefer to use generators instead of just lists? In what situations should I prefer a plain array? For example instead of this: emails = ...
8
votes
3answers
24k views

Browser-based GUI for a python application [closed]

I want to create a web/browser-based GUI for a command-line python application. The goal is to make use of HTML/JS technologies to create this GUI. As the application itself, it needs to run on Linux ...
8
votes
1answer
146 views

Importing Python modules at the time of use

Often in my own personal Python libraries, I do something like this: class MyClass: # ... def plot(self): import someGraphicsLibrary as graphicslib graphicslib.plot(self....
8
votes
4answers
4k views

Google App Engine, what are the advantages and limitations?

First, does anyone know a specific advantage of Google App Engine for Python Apps? Is there some advantage over Heruko or Appfog? And about the database API? Now, what about the limitations? Are ...
8
votes
3answers
1k views

Microservices and shared libraries

We are designing a system based on independent microservices (connected via a RabbitMq bus). The code will (for the first components at least) be written in python (both python2 and python3). We have ...
8
votes
1answer
3k views

Good technique for search text tokenization

We are looking for a way to tokenize some text in the same or similar way as a search engine would do it. The reason we are doing this is so that we can run some statistical analysis on the tokens. ...
8
votes
1answer
1k views

Accepted best practices for setup.py of a forked project

Context: I was looking to see if there was a tool to do something (in my case I was looking for a proxy that would allow for http log inspection from a python test framework). There turned out to be ...
8
votes
2answers
3k views

Caching factory design

I have a factory class XFactory that creates objects of class X. Instances of X are very large, so the main purpose of the factory is to cache them, as transparently to the client code as possible. ...
8
votes
1answer
723 views

Options to handle large (multi-gigabyte) file uploads

How would you implement a very large file upload functionality with a Django application and S3? In my side job as a photographer, I have several clients for which I have a need to share multi-...
8
votes
3answers
725 views

Refactoring of a client API for avoid duplicated code and unclear passage of parameters

I need to develop an API, the functions of the API are requests that call the service exposed by a server. Initially the API worked like this: class Server: def firstRequest(self, arg1, arg2): ...
8
votes
1answer
2k views

Making API calls with celery

I'm designing a system for a client where the requirements are: they upload a JSON file (one object/line) make a call to an API with the JSON object as the payload record the state (success/failure) ...