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
299 votes
2 answers
468k views

Python file naming convention?

I've seen this part of PEP-8 https://www.python.org/dev/peps/pep-0008/#package-and-module-names I'm not clear on whether this refers to the file name of a module/class/package. If I had one example ...
darkace's user avatar
  • 3,101
231 votes
9 answers
78k views

Why do people hesitate to use Python 3?

Python 3 was released in December 2008. A lot of time has passed since then but still today many developers hesitate to use Python 3. Even popular frameworks like Django are not compatible with Python ...
Ham Vocke's user avatar
  • 2,477
220 votes
8 answers
289k views

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

I have always been using this method: from sys import argv and use argv with just argv. But there is a convention of using this: import sys and using the argv by sys.argv The second method makes ...
Santosh Kumar's user avatar
172 votes
23 answers
26k views

Programming cleanly when writing scientific code

I don't really write large projects. I'm not maintaining a huge database or dealing with millions of lines of code. My code is primarily "scripting" type stuff - things to test mathematical ...
auden's user avatar
  • 1,637
160 votes
1 answer
109k views

Module vs. Package?

Whenever I do from 'x' import 'y' I was wondering which one is considered the 'module' and which is the 'package', and why it isn't the other way around?
Dark Templar's user avatar
  • 6,253
147 votes
24 answers
212k views

What are the drawbacks of Python? [closed]

Python seems all the rage these days, and not undeservingly - for it is truly a language with which one almost enjoys being given a new problem to solve. But, as a wise man once said (calling him a ...
137 votes
3 answers
62k views

Why Was Python Written with the GIL?

The global interpreter lock (GIL) seems to be often cited as a major reason why threading and the like is a touch tricky in Python - which raises the question "Why was that done in the first place?" ...
Fomite's user avatar
  • 2,626
135 votes
15 answers
34k 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 ...
Zen's user avatar
  • 1,693
119 votes
2 answers
119k views

What's wrong with relative imports in Python?

I recently upgraded versions of pylint, a popular Python style-checker. It has gone ballistic throughout my code, pointing out places where I import modules in the same package, without specifying the ...
Oddthinking's user avatar
  • 1,732
108 votes
10 answers
78k views

Why is Python used for high-performance/scientific computing (but Ruby isn't)?

There's a quote from a PyCon 2011 talk that goes: At least in our shop (Argonne National Laboratory) we have three accepted languages for scientific computing. In this order they are C/C++, ...
Cyclops's user avatar
  • 2,167
107 votes
8 answers
21k views

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

I've started reading the design pattern book by the GoF. Some patterns seem very similar with only minor conceptual differences. Do you think out of the many patterns some are unnecessary in a ...
Gere's user avatar
  • 2,191
105 votes
9 answers
28k views

Check First 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: Checking First vs Exception handling. Here is ...
jmq's user avatar
  • 6,068
101 votes
3 answers
127k views

Why is Python written in C and not in C++?

In Python's tutorial one can read that Python's original implementation is in C; On the other hand, the Python implementation, written in C, (...) I'm very curious why was Python written in C and ...
Piotr Dobrogost's user avatar
97 votes
5 answers
261k views

Why store a function inside a python dictionary?

I'm a python beginner, and I just learned a technique involving dictionaries and functions. The syntax is easy and it seems like a trivial thing, but my python senses are tingling. Something tells me ...
mdeutschmtl's user avatar
  • 1,089
96 votes
5 answers
230k 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 ...
Niklas Rosencrantz's user avatar
90 votes
5 answers
157k views

Is it a good practice to declare instance variables as None in a class in Python?

Consider the following class: class Person: def __init__(self, name, age): self.name = name self.age = age My coworkers tend to define it like this: class Person: name = None ...
Remco Haszing's user avatar
89 votes
6 answers
14k views

Why was Python's popularity so sudden? [closed]

Python first appeared in 1991, but it was somewhat unknown until 2004, if the TIOBE rankings quantify anything meaningful. What happened? What caused the interest in this 13 year old language to go ...
Eric Wilson's user avatar
  • 12.1k
87 votes
6 answers
111k views

Single quotes vs double quotes [closed]

I just started a job where I'm writing Python after coming from a Java background, and I'm noticing that other developers tend to quote strings using single quotes ('') instead of double quotes (""). ...
Eric Hydrick's user avatar
  • 2,451
85 votes
7 answers
25k views

How bad of an idea is it to use Python files as configuration files?

I've always used JSON files for configuration of my applications. I started using them from when I coded a lot of Java, and now I'm working mainly on server-side and data science Python development ...
André Christoffer Andersen's user avatar
83 votes
3 answers
42k views

How do I move away from the “for-loop” school of thought?

This is a rather conceptual question, but I was hoping I could get some good advice on this. A lot of the programming I do is with (NumPy) arrays; I often have to match items in two or more arrays ...
turnip's user avatar
  • 1,657
82 votes
5 answers
151k views

Is Python Interpreted or Compiled?

This is just a wondering I had while reading about interpreted and compiled languages. Ruby is no doubt an interpreted language since the source code is processed by an interpreter at the point of ...
crodjer's user avatar
  • 1,039
71 votes
6 answers
87k 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 ...
Dan Oberlam's user avatar
  • 1,281
61 votes
5 answers
50k views

Why doesn't Python allow multi-line lambdas?

Can someone explain the concrete reasons why BDFL choose to make Python lambdas single line? This is good: lambda x: x**x This results in an error: lambda x: x**x I understand that making ...
treecoder's user avatar
  • 9,485
60 votes
3 answers
96k views

What is the name of ** in python?

When programming Python I sometimes do a ** to make a conversion. I understand what it does but what data structures am I manipulating? A dict and what is the other? An array? Is there a name for the *...
Niklas Rosencrantz's user avatar
60 votes
5 answers
110k views

R vs Python for data analysis [closed]

I have been programming for about a year and I am really interested in data analysis and machine learning. I am taking part in a couple of online courses and am reading a couple of books. Everything ...
The_Cthulhu_Kid's user avatar
58 votes
6 answers
11k views

Why aren't there other programming languages that compile to Python bytecode?

In Java, there are multiple languages that compile to Java bytecode and can run on the JVM -- Clojure, Groovy, and Scala being the main ones I can remember off the top of my head. However, Python ...
Michael0x2a's user avatar
  • 1,099
58 votes
3 answers
114k views

Why Python and not Lua? [closed]

Why has Python been backed by google and become so rapidly popular and Lua has not? Do you know why Lua has stayed in background?
BenjaminB's user avatar
  • 1,706
57 votes
5 answers
96k views

Should I really use all uppercase for my constants?

I am a Python programmer primarily who uses pylint for linting source code. I am able to eliminate all of the warnings except one: Invalid name for a constant. Changing the name to all caps fixes it, ...
Abhishek Kumar's user avatar
56 votes
16 answers
51k views

Why such popularity with Python? [closed]

Other than being annoyed at whitespace as syntax, I'm not a hater, I just don't get the fascination with Python. I appreciate the poetry of Perl, and have programmed beautiful web services in bash &...
56 votes
5 answers
114k views

Is it considered Pythonic to have multiple classes defined in the same file?

In working with python for the first time, I've found that I end up writing multiple classes in the same file, which is opposed to other languages like Java, which uses one file per class. Usually, ...
Ampt's user avatar
  • 4,673
55 votes
4 answers
46k views

Why do iterators in Python raise an exception?

Here's the syntax for iterators in Java (somewhat similar syntax in C#): Iterator it = sequence.iterator(); while (it.hasNext()) { System.out.println(it.next()); } Which makes sense. Here's the ...
NullUserException's user avatar
54 votes
5 answers
37k views

Are Python mixins an anti-pattern?

I'm fully aware that pylint and other static analysis tools are not all-knowing, and sometimes their advice must be disobeyed. (This applies for various classes of messages, not just conventions.) If ...
cat's user avatar
  • 734
51 votes
4 answers
9k views

Python Forgiveness vs. Permission and Duck Typing

In Python, I often hear that it is better to "beg forgiveness" (exception catching) instead of "ask permission" (type/condition checking). In regards to enforcing duck typing in Python, is this try: ...
darkfeline's user avatar
  • 1,243
50 votes
5 answers
75k views

Purpose of `return self` from a class method?

I came across something like this in an open-source project. Methods that modify instance attributes return a reference to the instance. What is the purpose of this construct? class Foo(object): ...
nate c's user avatar
  • 797
50 votes
8 answers
10k views

Why is the worst case for this function O(n^2)?

I'm trying to teach myself how to calculate BigO notation for an arbitrary function. I found this function in a textbook. The book asserts that the function is O(n2). It gives an explanation as to why ...
SteveJ's user avatar
  • 636
48 votes
6 answers
130k views

How do I learn Python from zero to web development? [closed]

I am looking into learning Python for web development. Assuming I already have some basic web development experience with Java (JSP/Servlets), I'm already familiar with web design (HTML, CSS, JS), ...
Terence Ponce's user avatar
46 votes
8 answers
21k views

How can I learn to effectively write Pythonic code?

Doing a google search for "pythonic" reveals a wide range of interpretations. The wikipedia page says: A common neologism in the Python community is pythonic, which can have a wide range of ...
user avatar
44 votes
11 answers
7k views

Is C# development effectively inseparable from the IDE you use?

I'm a Python programmer learning C# who is trying to stop worrying and just love C# for what it is, rather than constantly comparing it back to Python. I'm caught up on one point: the lack of ...
Ghopper21's user avatar
  • 872
43 votes
13 answers
11k views

How does Python's handling of line-breaks differ from JavaScript's automatic semicolons?

Javascript has a feature called Automatic Semicolon Insertion where basically if the parser encounters an invalid token, and the last token before that was a line break, then the parser will insert a ...
Einar Egilsson's user avatar
43 votes
7 answers
12k views

Are small amounts of functional programming understandable by non-FP people? [closed]

Case: I'm working at a company, writing an application in Python that is handling a lot of data in arrays. I'm the only developer of this program at the moment, but it will probably be used/modified/...
kd35a's user avatar
  • 533
43 votes
3 answers
81k views

Why doesn't Python have a "flatten" function for lists?

Erlang and Ruby both come with functions for flattening arrays. It seems like such a simple and useful tool to add to a language. One could do this: >>> mess = [[1, [2]], 3, [[[4, 5]], 6]] &...
Hubro's user avatar
  • 676
43 votes
4 answers
34k views

Difference between a REPL and interactive shell

Noob question. I am not quite able to tell the difference between a REPL and an interactive shell just by reading the definitions on Wikipedia. Wiki notes that REPL is a particular kind of ...
tchakravarty's user avatar
43 votes
4 answers
20k views

@staticmethod vs module-level function

This is not about @staticmethod and @classmethod! I know how staticmethod works. What I want to know is the proper use cases for @staticmethod vs. a module-level function. I've googled this ...
darkfeline's user avatar
  • 1,243
41 votes
6 answers
10k views

Should I create a class if my function is complex and has a lot of variables?

This question is somewhat language-agnostic, but not completely, since Object Oriented Programming (OOP) is different in, for example, Java, which doesn't have first-class functions, than it is in ...
iCanLearn's user avatar
  • 1,311
41 votes
3 answers
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 ...
Koichi Sasada's user avatar
41 votes
3 answers
85k views

What are "class methods" and "instance methods", in Python?

There has been a discussion in chat relating to a question (the question itself being irrelevant to this one), that has revealed I may not know Python whatsoever. In my mind, although terminology ...
Lightness Races in Orbit's user avatar
41 votes
3 answers
16k views

Why are C++ template error messages so horrific?

C++ templates are notorious for generating long, unreadable error messages. I have a general idea of why template error messages in C++ are so bad. Essentially, the problem is that the error isn't ...
Channel72's user avatar
  • 2,485
41 votes
6 answers
36k views

Which style to use for unused return parameters in a Python function call

Is there any recommended/generally accepted coding style for handling situations where a function returns a tuple of values but only one of those values is used afterwards (note that this is mostly ...
user49643's user avatar
  • 521
39 votes
6 answers
45k 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 ...
Serge Ballesta's user avatar
39 votes
3 answers
29k views

Is it conventional to raise a NotImplementedError for methods whose implementation is pending, but not planned to be abstract?

I like to raise a NotImplementedError for any method that I want to implement, but where I haven't gotten around to doing it yet. I might already have a partial implementation, but prepend it with ...
gerrit's user avatar
  • 1,090

1
2 3 4 5
40