Imports in python are static, any solution?

Discussion in 'Python' started by Ravi, Apr 13, 2009.

  1. Ravi

    Ravi Guest

    foo.py :

    i = 10

    def fi():
    global i
    i = 99

    bar.py :

    import foo
    from foo import i

    print i, foo.i
    foo.fi()
    print i, foo.i

    This is problematic. Well I want i to change with foo.fi() .
    Ravi, Apr 13, 2009
    #1
    1. Advertising

  2. Quoting Ravi <>:

    >
    > This is problematic. Well I want i to change with foo.fi() .


    You can't. i and foo.i are _different_ variables that just happen to share the
    same value initially. What you are observing is no different than

    i = 10
    j = i
    i = 99

    print i # prints 99
    print j # print 10

    May I ask, why is it problematic? You should be avoiding the use of global
    variables anyway.

    --
    Luis Zarrabeitia
    Facultad de Matemática y Computación, UH
    http://profesores.matcom.uh.cu/~kyrie

    --
    Participe en Universidad 2010, del 8 al 12 de febrero de 2010
    La Habana, Cuba
    http://www.universidad2010.cu
    Luis Alberto Zarrabeitia Gomez, Apr 13, 2009
    #2
    1. Advertising

  3. Ravi

    David Stanek Guest

    On Mon, Apr 13, 2009 at 11:59 AM, Ravi <> wrote:
    > foo.py :
    >
    >    i = 10
    >
    >   def fi():
    >      global i
    >      i = 99
    >
    > bar.py :
    >
    >    import foo
    >    from foo import i
    >
    >    print i, foo.i
    >    foo.fi()
    >    print i, foo.i
    >
    > This is problematic. Well I want i to change with foo.fi() .


    Why not only import foo and using foo.i? In fi() when you set i = 99
    you are creating a new object called i in foo's namespace.


    --
    David
    blog: http://www.traceback.org
    twitter: http://twitter.com/dstanek
    David Stanek, Apr 13, 2009
    #3
  4. On Mon, 13 Apr 2009 08:59:19 -0700 (PDT), Ravi <>
    declaimed the following in gmane.comp.python.general:

    > foo.py :
    >
    > i = 10
    >
    > def fi():
    > global i
    > i = 99
    >
    > bar.py :
    >
    > import foo
    > from foo import i
    >
    > print i, foo.i
    > foo.fi()
    > print i, foo.i
    >
    > This is problematic. Well I want i to change with foo.fi() .


    The "i" in bar.py is effectively just a second name bound to the
    same object (the integer 10) that is bound to "i" in module foo.

    from foo import i

    is equivalent to

    import foo
    i = foo.i

    When foo.fi() is executed, it REBINDS foo's "i" to a new object
    (integer 99). The "i" in bar does not get rebound and still accesses the
    original object.

    This behavior is not something dependent upon the behavior of
    "import" -- it is the crux of all Python "assignment" behavior.

    What you are asking for is the equivalent of expecting

    a = 10
    b = a
    a = 99

    to change the value of "b"
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Apr 13, 2009
    #4
  5. Ravi

    norseman Guest

    AJ Mayorga wrote:
    > For something like this I generally create a superclass to hold
    > configuration variables that will change overtime, doing that will save you
    > from insanity.
    >
    > Class configVar:
    >
    > #set initial values
    > Def __init__(self):
    > Self.A = 5
    > Self.B = 10
    > Self.C = 20
    >
    >
    > Class myMath(configVars):
    > def __init__(self):
    > pass
    >
    > def SubAandB(self):
    > return self.A - self.B
    >
    > def AddCandB(self):
    > return self.C + self.B
    >
    > def MultiplyXbyA(self, x):
    > return self.A * x
    >
    >
    > m = myMath()
    > X = m.SubAandB()
    > Y = m.AddCandB()
    > Z = m.MultiplyXbyA(32)
    >
    > Keeps your vars in a safer easier to handle, debug, and change kinda way
    > Good luck
    >
    > AJ
    >
    > -----Original Message-----
    > From: python-list-bounces+aj=
    > [mailto:python-list-bounces+aj=] On Behalf Of David
    > Stanek
    > Sent: Monday, April 13, 2009 12:12 PM
    > To: Ravi
    > Cc:
    > Subject: Re: Imports in python are static, any solution?
    >
    > On Mon, Apr 13, 2009 at 11:59 AM, Ravi <> wrote:
    >> foo.py :
    >>
    >> i = 10
    >>
    >> def fi():
    >> global i
    >> i = 99
    >>
    >> bar.py :
    >>
    >> import foo
    >> from foo import i
    >>
    >> print i, foo.i
    >> foo.fi()
    >> print i, foo.i
    >>
    >> This is problematic. Well I want i to change with foo.fi() .

    >
    > Why not only import foo and using foo.i? In fi() when you set i = 99
    > you are creating a new object called i in foo's namespace.
    >
    >

    ===============================

    Aj is right. In foo.py there are two definitions for 'i'. The initial
    and the replacement initiated by fi(). While initially there is no 'i'
    definition in bar itself.

    To test, use my changes to bar.py

    import foo
    #from foo import i

    i= foo.i
    print i, foo.i
    x= foo.fi()
    print i, x, foo.i
    x= foo.i
    print i, x, foo.i

    the output will be:
    10 10
    10 None 99
    10 99 99

    output is same if you uncomment #from... and comment i=...
    The '...import i' creates the "same" var as the i=... in the current run
    If you comment out both the from and the i= then the print i will fail
    because i has not been defined in current space.
    foo.fi() returns None (nothing) per it's definition.
    whereas the first foo.i returns the initial i value and the foo.i after
    foo.fi() returns the 2nd value, foo's i reset to 99 by fi() inside foo.

    Clear as Mud???


    Steve
    norseman, Apr 13, 2009
    #5
  6. Ravi

    AJ Mayorga Guest

    For something like this I generally create a superclass to hold
    configuration variables that will change overtime, doing that will save you
    from insanity.

    Class configVar:

    #set initial values
    Def __init__(self):
    Self.A = 5
    Self.B = 10
    Self.C = 20


    Class myMath(configVars):
    def __init__(self):
    pass

    def SubAandB(self):
    return self.A - self.B

    def AddCandB(self):
    return self.C + self.B

    def MultiplyXbyA(self, x):
    return self.A * x


    m = myMath()
    X = m.SubAandB()
    Y = m.AddCandB()
    Z = m.MultiplyXbyA(32)

    Keeps your vars in a safer easier to handle, debug, and change kinda way
    Good luck

    AJ

    -----Original Message-----
    From: python-list-bounces+aj=
    [mailto:python-list-bounces+aj=] On Behalf Of David
    Stanek
    Sent: Monday, April 13, 2009 12:12 PM
    To: Ravi
    Cc:
    Subject: Re: Imports in python are static, any solution?

    On Mon, Apr 13, 2009 at 11:59 AM, Ravi <> wrote:
    > foo.py :
    >
    >    i = 10
    >
    >   def fi():
    >      global i
    >      i = 99
    >
    > bar.py :
    >
    >    import foo
    >    from foo import i
    >
    >    print i, foo.i
    >    foo.fi()
    >    print i, foo.i
    >
    > This is problematic. Well I want i to change with foo.fi() .


    Why not only import foo and using foo.i? In fi() when you set i = 99
    you are creating a new object called i in foo's namespace.


    --
    David
    blog: http://www.traceback.org
    twitter: http://twitter.com/dstanek
    --
    http://mail.python.org/mailman/listinfo/python-list
    AJ Mayorga, Apr 15, 2009
    #6
  7. Ravi

    Ravi Guest

    On Apr 14, 1:23 am, norseman <> wrote:
    > AJ Mayorga wrote:
    > > For something like this I generally create a superclass to hold
    > > configuration variables that will change overtime, doing that will save you
    > > from insanity.

    >
    > > Class configVar:

    >
    > >    #set initial values
    > >    Def __init__(self):
    > >            Self.A = 5
    > >            Self.B = 10
    > >            Self.C = 20

    >
    > > Class myMath(configVars):
    > >    def __init__(self):
    > >            pass

    >
    > >    def SubAandB(self):
    > >            return self.A - self.B

    >
    > >    def AddCandB(self):
    > >            return self.C + self.B

    >
    > >    def MultiplyXbyA(self, x):
    > >            return self.A * x

    >
    > > m = myMath()
    > > X = m.SubAandB()
    > > Y = m.AddCandB()
    > > Z = m.MultiplyXbyA(32)

    >
    > > Keeps your vars in a safer easier to handle, debug, and change kinda way
    > > Good luck

    >
    > > AJ

    >
    > > -----Original Message-----
    > > From: python-list-bounces+aj=
    > > [mailto:python-list-bounces+aj=] On Behalf Of David
    > > Stanek
    > > Sent: Monday, April 13, 2009 12:12 PM
    > > To: Ravi
    > > Cc:
    > > Subject: Re: Imports in python are static, any solution?

    >
    > > On Mon, Apr 13, 2009 at 11:59 AM, Ravi <> wrote:
    > >> foo.py :

    >
    > >>    i = 10

    >
    > >>   def fi():
    > >>      global i
    > >>      i = 99

    >
    > >> bar.py :

    >
    > >>    import foo
    > >>    from foo import i

    >
    > >>    print i, foo.i
    > >>    foo.fi()
    > >>    print i, foo.i

    >
    > >> This is problematic. Well I want i to change with foo.fi() .

    >
    > > Why not only import foo and using foo.i? In fi() when you set i = 99
    > > you are creating a new object called i in foo's namespace.

    >
    > ===============================
    >
    > Aj is right. In foo.py there are two definitions for 'i'. The initial
    > and the replacement initiated by fi(). While initially there is no 'i'
    > definition in bar itself.
    >
    > To test, use my changes to bar.py
    >
    > import foo
    > #from foo import i
    >
    > i= foo.i
    > print i, foo.i
    > x= foo.fi()
    > print i, x, foo.i
    > x= foo.i
    > print  i, x, foo.i
    >
    > the output will be:
    > 10 10
    > 10 None 99
    > 10 99 99
    >
    > output is same if you uncomment #from... and comment i=...
    > The '...import i' creates the "same" var as the i=... in the current run
    > If you comment out both the from and the i= then the print i will fail
    > because i has not been defined in current space.
    > foo.fi() returns None (nothing) per it's definition.
    > whereas the first foo.i returns the initial i value and the foo.i after
    > foo.fi() returns the 2nd value, foo's i reset to 99 by fi() inside foo.
    >
    > Clear as Mud???
    >
    > Steve


    Yes I find the difference. Thank you all.
    Ravi, Apr 16, 2009
    #7
    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. Kenneth P. Turvey

    Static imports in JSP pages

    Kenneth P. Turvey, Oct 30, 2007, in forum: Java
    Replies:
    15
    Views:
    1,303
  2. Replies:
    5
    Views:
    927
    Robert Klemme
    Dec 16, 2007
  3. Albert
    Replies:
    4
    Views:
    10,859
    Albert
    Jul 10, 2008
  4. zildjohn01
    Replies:
    0
    Views:
    644
    zildjohn01
    Feb 22, 2011
  5. Victor Hooi
    Replies:
    1
    Views:
    105
    Devin Jeanpierre
    Nov 25, 2013
Loading...

Share This Page