Learning inheritance

Niklasro niklasro at gmail.com
Sun Sep 19 03:13:04 EDT 2010


On Sep 18, 11:15 pm, Jorgen Grahn <grahn+n... at snipabacken.se> wrote:
> 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   .

Thanks for sharing the knowledge. I alternatively think about
declaring the variable in a setting.py file and import it. It doesn't
create many objects but I want to learn more professional code
conventions than same test repeated.
Sincerely,
Niklas R



More information about the Python-list mailing list