#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    28
    Rep Power
    0

    Variables question


    When requiring or including files in PHP, you basically put the code from that file there. But when importing in Python, you don't because you can't access variables as you would in PHP. Well I am making a config file that will be using the ConfigParse module from python. But what's the best way of putting that configparse object global? It's kind of an MVC framework without the view aspect that I'm using.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,843
    Rep Power
    480
    Lets suppose we have a file p.py and in p.py is a module scope variable tolerance and it's value is 1e-7

    tolerance = 1e-7


    In some other module we have

    import p
    #now we can access
    p.tolerance
    #and even change it
    p.tolerance = 1e-15



    Hope this helps.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    28
    Rep Power
    0

    Thanks


    Originally Posted by b49P23TIvg
    Lets suppose we have a file p.py and in p.py is a module scope variable tolerance and it's value is 1e-7

    tolerance = 1e-7


    In some other module we have

    import p
    #now we can access
    p.tolerance
    #and even change it
    p.tolerance = 1e-15



    Hope this helps.
    I am using this right now, I just hoped that there was a better solution. Thanks though.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    431
    Rep Power
    67
    Originally Posted by Dreeass
    I am using this right now, I just hoped that there was a better solution. Thanks though.
    There is no better solution, but a different one exists. Borrowing b49P23TIvg’s example:

    Code:
    from p import *
    #now we can access
    tolerance
    #and even change it
    tolerance = 1e-15
    You should be very careful if you decide to use this. Namespaces are there for a reason.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    28
    Rep Power
    0

    Arrow


    Originally Posted by SuperOscar
    There is no better solution, but a different one exists. Borrowing b49P23TIvg’s example:

    Code:
    from p import *
    #now we can access
    tolerance
    #and even change it
    tolerance = 1e-15
    You should be very careful if you decide to use this. Namespaces are there for a reason.
    I am not using import *, I know why you shouldn't.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    I am not using import *, I know why you shouldn't.
    Strange, as this seems to be exactly what you are asking for.

    You can also import specific names using the from syntax.

    As in:
    Code:
    from my_module import my_variable
    This has the same warnings about namespace polution as the * imports but it is slightly less evil. In this case one can at least see where a name came from in your code. Overuse of it is still highly discouraged.

    -Mek
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by Mekire
    Strange, as this seems to be exactly what you are asking for.

    You can also import specific names using the from syntax.

    As in:
    Code:
    from my_module import my_variable
    This has the same warnings about namespace polution as the * imports but it is slightly less evil. In this case one can at least see where a name came from in your code. Overuse of it is still highly discouraged.

    -Mek
    I was actually just asking for something different than the import, I could always try.

IMN logo majestic logo threadwatch logo seochat tools logo