Learning inheritance

Discussion in 'Python' started by Niklasro, Sep 18, 2010.

  1. Niklasro

    Niklasro Guest

    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?
    Thanks
    Niklas
     
    Niklasro, Sep 18, 2010
    #1
    1. Advertising

  2. Niklasro

    Guest

    On 18 sep, 17:25, 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']


    First learn to use Python correctly:

    url = os.environ.get("HTTP_HOST", os.environ["SERVER_NAME"])

    => dict.get(key, default=None)

    Also and FWIW, neither HTTP_HOST not SERVER_NAME are really urls...

    > I repeat for many methods. So declaring it
    > to a super class and inheriting it is my plan. Do you agree or propose
    > otherwise?


    Not enough background to answer.
     
    , Sep 18, 2010
    #2
    1. Advertising

  3. Niklasro

    Jorgen Grahn Guest

    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 .
     
    Jorgen Grahn, Sep 19, 2010
    #3
  4. Niklasro

    Niklasro Guest

    On Sep 18, 4:13 pm, ""
    <> wrote:
    > On 18 sep, 17:25, 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']

    >
    > First learn to use Python correctly:
    >
    > url = os.environ.get("HTTP_HOST", os.environ["SERVER_NAME"])
    >
    > => dict.get(key, default=None)
    >
    > Also and FWIW, neither HTTP_HOST not SERVER_NAME are really urls...
    >
    > > I repeat for many methods. So declaring it
    > > to a super class and inheriting it is my plan. Do you agree or propose
    > > otherwise?

    >
    > Not enough background to answer.


    Thanks for replying and informing correctness. More background is the
    variable I want accessible for many functions and files either is
    HTTP_HOST or SERVER_NAME used as beginning of url or resource locator
    indicated where the software is used. Instead of declaring the
    variable many times feasibility study is how to minify number of times
    I declare the same variable. I got 2 files main.py and i18n both with
    webapp request handlers which I would like access the variable.
    Thanks
    Niklas R
     
    Niklasro, Sep 19, 2010
    #4
  5. Niklasro

    Carl Banks Guest

    On Sep 18, 4:15 pm, Jorgen Grahn <> 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.


    That statement might be a little too general to be true. Inheritance
    is a (or the) main tool for different objects to share behavior, which
    is implemented by code. So when your program is organized around
    objects, it is the (or a) main tool.

    You are right that you shouldn't rework your code into an OO style
    simply because you want to share code. I think that's what you meant.


    > Just make it a
    > function and place it in one of your modules (files):


    Or even just make it a global variable in the module (which would work
    in this case, unless you plan to update the environment within the
    program).


    Carl Banks
     
    Carl Banks, Sep 19, 2010
    #5
  6. Niklasro

    alex23 Guest

    Niklasro <> wrote:
    > I got 2 files main.py and i18n both with
    > webapp request handlers which I would like access the variable.


    I'd probably use a module for this. Create a third file, called
    something like shared.py, containing the line that bruno gave above:

    url = os.environ.get("HTTP_HOST", os.environ["SERVER_NAME"])

    Then from within both main & i18n you can 'import shared' and access
    the variable as 'shared.url'.

    Python only actually executes a module the first time it's imported,
    every other import will be given a reference to the same module
    object. This also lets you share temporary data between modules. Any
    module that imports 'shared' can add an attribute to it ('shared.foo =
    "barbaz"') that will be visible to all other modules that have (or
    will have) imported it.
     
    alex23, Sep 19, 2010
    #6
  7. Niklasro

    Niklasro Guest

    On Sep 18, 11:15 pm, Jorgen Grahn <> 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
     
    Niklasro, Sep 19, 2010
    #7
  8. Niklasro

    Niklasro Guest

    On Sep 19, 2:31 am, alex23 <> wrote:
    > Niklasro <> wrote:
    > > I got 2 files main.py and i18n both with
    > > webapp request handlers which I would like access the variable.

    >
    > I'd probably use a module for this. Create a third file, called
    > something like shared.py, containing the line that bruno gave above:
    >
    > url = os.environ.get("HTTP_HOST", os.environ["SERVER_NAME"])
    >
    > Then from within both main & i18n you can 'import shared' and access
    > the variable as 'shared.url'.
    >
    > Python only actually executes a module the first time it's imported,
    > every other import will be given a reference to the same module
    > object. This also lets you share temporary data between modules. Any
    > module that imports 'shared' can add an attribute to it ('shared.foo =
    > "barbaz"') that will be visible to all other modules that have (or
    > will have) imported it.


    I try a file setting.py declaring it like this just loose in the file
    not knowing much theory about it thinking it's easy and intuitive.
    Thanks
    Niklas
     
    Niklasro, Sep 19, 2010
    #8
  9. Niklasro

    Niklasro Guest

    It works but I don't know whether it's formally inheritance or class
    variable.

    Before code was
    url = os.environ['HTTP_HOST'] if os.environ.get('HTTP_HOST') else
    os.environ['SERVER_NAME']
    if url.find('niklas') > 0:

    and now the change saves me from repeating myself!

    util.py:
    url = os.environ.get("HTTP_HOST", os.environ["SERVER_NAME"]) #declared
    as class variable(?)

    And viola just test if util.url.find('niklas') > 0:

    Exactly what I wanted to do with your experienced guidance.

    Many thanks
    Happy refactored
     
    Niklasro, Sep 19, 2010
    #9
  10. On 2010-09-19 09:22, Niklasro wrote:
    > util.py:
    > url = os.environ.get("HTTP_HOST", os.environ["SERVER_NAME"]) #declared
    > as class variable(?)
    >

    There is no class here, so this is no class variable, and you're not
    inheriting anything. You're simply using a module.

    > And viola just test if util.url.find('niklas') > 0:
    >
    > Exactly what I wanted to do with your experienced guidance.
    >
    > Many thanks
    > Happy refactored
    >
     
    Thomas Jollans, Sep 19, 2010
    #10
  11. Niklasro

    Niklasro Guest

    On Sep 19, 8:12 am, Thomas Jollans <> wrote:
    > On 2010-09-19 09:22, Niklasro wrote:> util.py:
    > > url = os.environ.get("HTTP_HOST", os.environ["SERVER_NAME"]) #declared
    > > as class variable(?)

    >
    > There is no class here, so this is no class variable, and you're not
    > inheriting anything. You're simply using a module.
    >
    > > And viola just test if util.url.find('niklas') > 0:

    >
    > > Exactly what I wanted to do with your experienced guidance.

    >
    > > Many thanks
    > > Happy refactored

    >
    >

    Good to learn what I'm doing :) since important being able to explain
    choices taken farther than "doing it because it works".
    I understand the concept of modules may not correspond to java
    programming where I come from.
    Sincerely with thanks for the help,
    Niklas
     
    Niklasro, Sep 19, 2010
    #11
  12. alex23 a écrit :
    >
    > Python only actually executes a module the first time it's imported,


    Beware of multithreading and modules imported under different names...
    There can be issues with both in some web frameowrks.
     
    Bruno Desthuilliers, Sep 20, 2010
    #12
  13. Niklasro a écrit :
    > Good to learn what I'm doing :) since important being able to explain
    > choices taken farther than "doing it because it works".
    > I understand the concept of modules may not correspond to java
    > programming where I come from.


    Coming from Java - and specially if you only have experience with Java
    -, you'll have to unlearn quite a few things. Python is 100% object - in
    that everything you can bind to a name is an object, including classes,
    functions, methods, and even modules - but it doesn't try to force you
    into using classes when you don't need them.
     
    Bruno Desthuilliers, Sep 20, 2010
    #13
  14. Niklasro

    alex23 Guest

    Bruno Desthuilliers <>
    wrote:
    > alex23 a écrit :
    > > Python only actually executes a module the first time it's imported,

    >
    > Beware of multithreading and modules imported under different names...
    > There can be issues with both in some web frameowrks.


    Good points, Bruno, thank you.

    Niklasro, a good example of Bruno's second point: running a module as
    a script and then importing it elsewhere later will execute the module
    in the second import, creating two module objects - '__main__' and
    '<modulename>'.

    The issue with threading is the more important one of which to be
    aware.
     
    alex23, Sep 21, 2010
    #14
  15. Niklasro

    Niklasro Guest

    On Sep 20, 7:39 am, Bruno Desthuilliers <bruno.
    > wrote:
    > Niklasro a écrit :
    >
    > > Good to learn what I'm doing :) since important being able to explain
    > > choices taken farther than "doing it because it works".
    > > I understand the concept of modules may not correspond to java
    > > programming where I come from.

    >
    > Coming from Java - and specially if you only have experience with Java
    > -, you'll have to unlearn quite a few things. Python is 100% object - in
    > that everything you can bind to a name is an object, including classes,
    > functions, methods, and even modules - but it doesn't try to force you
    > into using classes when you don't need them.


    Which is good since always questioning the empty declarations Java has
    we know empty getters and setters forced to a class and interfaces
    with nothing but names and no logic. With this respect I prefer python
    solving same problem with ½ MB python 30 MB J2EE used to with drawback
    only that Java had the faster physical response. You can have the
    buggiest code respond the fastest like a hijacked environment
    physically boosted you don't want and naturally choosing the slower
    physical response time in favor of decent development environment.
    Thanks
    Niklas
     
    Niklasro, Sep 25, 2010
    #15
  16. Niklasro

    Niklasro Guest

    On Sep 21, 1:30 am, alex23 <> wrote:
    > Bruno Desthuilliers <>
    > wrote:
    >
    > > alex23 a écrit :
    > > > Python only actually executes a module the first time it's imported,

    >
    > > Beware of multithreading and modules imported under different names...
    > > There can be issues with both in some web frameowrks.

    >
    > Good points, Bruno, thank you.
    >
    > Niklasro, a good example of Bruno's second point: running a module as
    > a script and then importing it elsewhere later will execute the module
    > in the second import, creating two module objects - '__main__' and
    > '<modulename>'.
    >
    > The issue with threading is the more important one of which to be
    > aware.


    I follow it means learning when constructors get called twice.
    Normally a constructor should get called once only.
    Many thanks for the insights both solving my problem and referencing
    important topics
     
    Niklasro, Sep 25, 2010
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,197
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    853
    cppsks
    Oct 27, 2004
  3. Hal Vaughan
    Replies:
    7
    Views:
    530
  4. karthikbalaguru
    Replies:
    9
    Views:
    1,068
  5. Andrey Popp

    [I'm learning C]: Learning to use ucontext

    Andrey Popp, Jan 29, 2012, in forum: C Programming
    Replies:
    5
    Views:
    807
    Keith Thompson
    Jan 31, 2012
Loading...

Share This Page