fuzzy notepad

Python FAQ

I lurk in #python. It gets a lot of questions that are, shall we say, frequently asked. This is my attempt to catalogue interesting and useful questions. The answers will gradually become separate posts—perhaps on other blogs if someone else gets to them first. Let me know if there should be other questions, if the answers are unclear, or the answers have bugs in them!

Variables, operators, and functions

Classes, objects, and data

  • How do I make a tuple with one element? What’s this trailing comma?
  • How do I check what type a variable is?
  • Why can’t I just do super(type(self), self)? How does super work?
  • What’s the difference between tuples and lists?
  • What’s the difference between classmethod and staticmethod?
  • Why can’t I put attribute defaults at class level? Why doesn’t my class counter work?
  • How do I make a private attribute? What’s __foo do?
  • How do I inherit from a built-in class, like list or dict?
  • Why don’t my imports work?
  • How do I change the value of a global in another module?
  • Why do I need to specify self? How do I take a reference to an object’s method?
  • How does @property work? Why does it call my __getattr__? What’s a “descriptor”?
  • How do I make a decorator with optional arguments? How do decorators work?
  • Metaclasses are insane!

Libraries and the real world

  • I come from C/Java/Prolog/something. How do I write “natural”-looking Python?
  • I only know PHP. How do I write a Web application in Python?
  • Okay, great; now how do I deploy it?
  • How do I parse HTML?
  • How do I use a single parameter with the DB-API?
  • How do I write a networking application? How do I use sockets?
  • How do I do networking with a GUI?
  • How do I use threads?
  • How do I write a plugin system? How do I reload a module at runtime?
  • How do I write an IRC bot?

Security

  • How do I store user passwords? How do I do anything related to encryption?
  • How do I supply a password for an SSH connection or sudo?
  • How do I encrypt my program’s source code?

Bonus question

  • What are the worst features in Python?

Comments