Accessing variables in __init__.py

Discussion in 'Python' started by Gaudha, Oct 16, 2012.

  1. Gaudha

    Gaudha Guest

    my_package/
    __init__.py
    my_module1.py
    my_module2.py
    variables.py

    I want to define common variables in __init__.py and use the namespace in my_module1.py or my_module2.py. Defining it is not a problem. How can call it from my modules?

    If I define them in a module (say, variables.py), I can call them by importing variables.py in other modules. How can it be done if I define it in __init__.py?

    It may be a silly query as I am newbie in Python. But, I would be grateful to get help.
     
    Gaudha, Oct 16, 2012
    #1
    1. Advertising

  2. Gaudha

    Marco Nawijn Guest

    On Tuesday, October 16, 2012 10:48:17 AM UTC+2, Gaudha wrote:
    > my_package/
    >
    > __init__.py
    >
    > my_module1.py
    >
    > my_module2.py
    >
    > variables.py
    >
    >
    >
    > I want to define common variables in __init__.py and use the namespace in my_module1.py or my_module2.py. Defining it is not a problem. How can call it from my modules?
    >
    >
    >
    > If I define them in a module (say, variables.py), I can call them by importing variables.py in other modules. How can it be done if I define it in __init__.py?
    >
    >
    >
    > It may be a silly query as I am newbie in Python. But, I would be grateful to get help.


    Hi,

    If you store the variables in __init__.py, you can import them from the package. So in your case suppose __init__.py contains:
    a = 10
    b = {1 :"Hello", 2: "World" }

    Than if you import my_package, you can access the variables as follows (interactive IPython session):

    In [1]: import my_package

    In [2]: my_pack
    my_package my_package/

    In [2]: my_package.
    my_package.a my_package.b

    In [2]: my_package.a
    Out[2]: 10

    In [3]: my_package.b
    Out[3]: {1: 'Hello', 2: 'World'}

    In [4]:
     
    Marco Nawijn, Oct 16, 2012
    #2
    1. Advertising

  3. Gaudha

    Gaudha Guest

    On Tuesday, October 16, 2012 3:41:55 PM UTC+5:30, Marco Nawijn wrote:
    > On Tuesday, October 16, 2012 10:48:17 AM UTC+2, Gaudha wrote:
    >
    > > my_package/

    >
    > >

    >
    > > __init__.py

    >
    > >

    >
    > > my_module1.py

    >
    > >

    >
    > > my_module2.py

    >
    > >

    >
    > > variables.py

    >
    > >

    >
    > >

    >
    > >

    >
    > > I want to define common variables in __init__.py and use the namespace in my_module1.py or my_module2.py. Defining it is not a problem. How can call it from my modules?

    >
    > >

    >
    > >

    >
    > >

    >
    > > If I define them in a module (say, variables.py), I can call them by importing variables.py in other modules. How can it be done if I define it in __init__.py?

    >
    > >

    >
    > >

    >
    > >

    >
    > > It may be a silly query as I am newbie in Python. But, I would be grateful to get help.

    >
    >
    >
    > Hi,
    >
    >
    >
    > If you store the variables in __init__.py, you can import them from the package. So in your case suppose __init__.py contains:
    >
    > a = 10
    >
    > b = {1 :"Hello", 2: "World" }
    >
    >
    >
    > Than if you import my_package, you can access the variables as follows (interactive IPython session):
    >
    >
    >
    > In [1]: import my_package
    >
    >
    >
    > In [2]: my_pack
    >
    > my_package my_package/
    >
    >
    >
    > In [2]: my_package.
    >
    > my_package.a my_package.b
    >
    >
    >
    > In [2]: my_package.a
    >
    > Out[2]: 10
    >
    >
    >
    > In [3]: my_package.b
    >
    > Out[3]: {1: 'Hello', 2: 'World'}
    >
    >
    >
    > In [4]:


    Yea. I got it. It was a new information for me. A module in a package can import its own mother package to call the variables in __init__.

    Is it funny or an extraordinary feature? Anyway. I felt it as something weird. Guido should have done it something like how 'self' behaves in classes.
     
    Gaudha, Oct 16, 2012
    #3
    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. dwok
    Replies:
    7
    Views:
    712
    Hal Rosser
    Mar 4, 2005
  2. Alexander Eberts
    Replies:
    3
    Views:
    327
    Ulrich Petri
    Jul 15, 2003
  3. Steven Bethard
    Replies:
    2
    Views:
    463
    Steven Bethard
    Feb 16, 2005
  4. Kent Johnson
    Replies:
    7
    Views:
    923
    Jan Niklas Fingerle
    Feb 12, 2006
  5. Ramchandra Apte
    Replies:
    17
    Views:
    350
    Manuel Pégourié-Gonnard
    Sep 30, 2012
Loading...

Share This Page