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.

2004 questions
222
votes
8 answers

'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 the code self documented and I (really) adhere to…
90
votes
5 answers

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 age = None def __init__(self, name,…
43
votes
4 answers

@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 question, and it seems there's some general agreement…
darkfeline
  • 1,243
38
votes
14 answers

How is Python used in the real world?

I'm looking to get a job as a Python programmer. I know the basics of the language and have created a few games with it using pygame. I've also started to experiment with Django. However, looking at the job market, it doesn't seem very many Python…
sq1020
  • 579
35
votes
4 answers

How to deal with database connections in a Python library module

I have created a library in Python that contains functions for accessing a database. This is a wrapper library around a third party application database, written due to the fact that the third party application does not offer a decent API. Now I…
leancz
  • 481
  • 1
  • 4
  • 7
27
votes
3 answers

Python - 'if foo in dict' vs 'try: dict[foo]'

This is less a question about the nature of duck typing and more about staying pythonic, I suppose. First of all - when dealing with dicts, in particular when the structure of the dict is fairly predictable and a given key is not typically present…
user112358
25
votes
7 answers

Should we use python 2.6 or 2.7 or 3.x?

The version of python which I am using is 2.6, and there is a 2.7 and 3.x. Usually I use python for some trivial program/snippet. I realize there are some major difference between 2.x and 3.x. I would really like to know, if I am going to make a…
lamwaiman1988
  • 1,483
  • 2
  • 16
  • 22
24
votes
2 answers

Is **kwargs an antipattern?

We have a lot of code in our internal codebase that calls our libraries internally - these libraries often have a lot of arguments (think matplotlib) and our code is often doing only a specific task and simply passes the **kwargs on to the next…
22
votes
9 answers

I feel stuck in the center of Python, How to get past beginner

I really apologize if this doesn't follow the S.O rules but I need a little help, I personally still classify myself as a beginner in python, Yet I've wrote a very small and VERY SURE impractical program for my boss to use. I know I'm still a…
Isov5
  • 351
22
votes
7 answers

Python mutable default argument: Why?

I know that default arguments are created at the function initialisation time and not every time the function is called. See the following code: def ook (item, lst=[]): lst.append(item) print 'ook', lst def eek (item, lst=None): if lst…
21
votes
7 answers

For what types of applications is Python a bad choice?

I just started learning Python, and I'd like to get some more context on the language. I realize that, in many cases, Python is a slow language relative to C or C++. Thus, Python is probably not the best choice for applications that need to run as…
Casey Patton
  • 5,241
20
votes
5 answers

Is there any one standard framework for developing Python GUI apps.?

There are so many frameworks for writing GUI application using Python. But is there any one key standard framework? For example we have a bundle of .NET/C# on Visual Studio. I am thinking in other perspectives also. In future if I give an interview…
RPK
  • 4,378
18
votes
5 answers

Strategy for keeping up with (Python) language changes

Writing code that will still run years from now Programming languages change. Libraries change. Some code from 5, 10, or even 20 years ago might still run and produce expected results, whereas some code from 2 years might fail with a syntax…
gerrit
  • 1,090
  • 1
  • 8
  • 21
17
votes
2 answers

Is there a canonical way to cache instance methods in python?

I have some computationally intensive functions in my python script that I would like to cache. I went looking for solutions on stack overflow and found lots of links:…
16
votes
2 answers

What is the relationship between scope and namespaces in Python?

In many resources I found "scope" and "namespaces" are used interchangeably, which seems a bit confusing since they mean different things. Scope defines the region of the code where a name is available. The LEGB rule defines the way names are…
Nikos
  • 265
1
2 3
9 10