Learning inheritance

Jorgen Grahn grahn+nntp at snipabacken.se
Sat Sep 18 19:15:05 EDT 2010


On Sat, 2010-09-18, Niklasro wrote:
> Hi
> How can I make the visibility of a variable across many methods or
> files? To avoid repeating the same line eg     url =
> os.environ['HTTP_HOST'] if os.environ.get('HTTP_HOST') else
> os.environ['SERVER_NAME'] I repeat for many methods. So declaring it
> to a super class and inheriting it is my plan. Do you agree or propose
> otherwise?

Inheritance is not the main tool for sharing code. Just make it a
function and place it in one of your modules (files):

def get_host():
   """Return the environment's $HTTP_HOST if
   it exists, otherwise $SERVER_NAME or (if that
   doesn't exist either) None.
   """
   ...

Perhaps you are focusing too much on inheritance in general.
I personally almost never use it in Python -- it has much fewer
uses here than in staticaly typed languages.

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .  .
\X/     snipabacken.se>   O  o   .



More information about the Python-list mailing list