Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52670 > unrolled thread

refresing the edited python function

Started bySudheer Joseph <sudheer.joseph@yahoo.com>
First post2013-08-19 08:55 +0800
Last post2013-08-20 15:08 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  refresing the edited python function Sudheer Joseph <sudheer.joseph@yahoo.com> - 2013-08-19 08:55 +0800
    Re: refresing the edited python function alex23 <wuwei23@gmail.com> - 2013-08-20 17:01 +1000
      RE: refresing the edited python function "Prasad, Ramit" <ramit.prasad@jpmorgan.com.dmarc.invalid> - 2013-08-20 15:08 +0000

#52670 — refresing the edited python function

FromSudheer Joseph <sudheer.joseph@yahoo.com>
Date2013-08-19 08:55 +0800
Subjectrefresing the edited python function
Message-ID<mailman.10.1376875558.19984.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw


Hi,
I have been using ipython and ipython with qtconsole and working on a code with functions. Each time I make a modification in function  

I have to quit IPTHON console (in both with and with out qt console ) and reload the function freshly. If I need to see the changed I made in the function. I tried below options
del function name

import the module again  by issuing "from xxx.py import yy"
import xxx.py
make changes
reload(xxx.py)
this
 works only if the the function in the code has same name as the code. 
But even this do not reflect the changes made by editing the code.
So what is the standard way to update the function for further tests after an edit?
with best regards,
Sudheer 
 
***************************************************************
Sudheer Joseph 
Indian National Centre for Ocean Information Services
Ministry of Earth Sciences, Govt. of India
POST BOX NO: 21, IDA Jeedeemetla P.O.
Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55
Tel:+91-40-23886047(O),Fax:+91-40-23895011(O),
Tel:+91-40-23044600(R),Tel:+91-40-9440832534(Mobile)
E-mail:sjo.India@gmail.com;sudheer.joseph@yahoo.com
Web- http://oppamthadathil.tripod.com
***************************************************************

[toc] | [next] | [standalone]


#52720

Fromalex23 <wuwei23@gmail.com>
Date2013-08-20 17:01 +1000
Message-ID<kuv48f$ed5$1@dont-email.me>
In reply to#52670
On 19/08/2013 10:55 AM, Sudheer Joseph wrote:
> I have been using ipython and ipython with qtconsole and working on a
> code with functions. Each time I make a modification in function
> I have to quit IPTHON console (in both with and with out qt console )
> and reload the function freshly. If I need to see the changed I made in
> the function. I tried below options

> del function name
> import the module again  by issuing "from xxx.py import yy"

This doesn't re-import the module if xxx has already been imported. It 
simply rebinds xxx.yy to yy.

> import xxx.py

This also doesn't re-import the module if it has already been imported.

When you import a module, or a function from a module, a module object 
is created and stored in sys.modules. Any subsequent 'import <module>' 
calls will return a reference to that module object, and won't reload 
from file at all.

You can easily verify this by creating a test module 'foo' with a single 
line of `print('loading foo')` and then trying this from the console:

     In [1]: import foo
     loading foo

     In [2]: del foo

     In [3]: import foo

     In [4]:

Note that you only see 'loading foo' the first time you import the 
module. In order to have the module loaded again rather than returning 
the existing reference, you would use `reload(foo)`:

     In [5]: reload(foo)
     loading foo

So: in order to be able to use functions from a re-loaded module, you 
should always refer to them via the module object, and not import them 
directly:

     >>> import xxx
     >>> xxx.yy() # original code
     # ...modify function `yy` in your source file
     >>> reload(xxx)
     >>> xxx.yy() # new code

Or: you can reload the module and then rebind the functions:

     >>> from xxx import yy
     >>> yy() # original code
     # ...modify function `yy` in your source file
     >>> reload(xxx)
     >>> from xxx import yy
     >>> yy() # new code

Hope this helps.

[toc] | [prev] | [next] | [standalone]


#52737

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com.dmarc.invalid>
Date2013-08-20 15:08 +0000
Message-ID<mailman.61.1377013094.19984.python-list@python.org>
In reply to#52720
alex23
> 
> On 19/08/2013 10:55 AM, Sudheer Joseph wrote:
> > I have been using ipython and ipython with qtconsole and working on a
> > code with functions. Each time I make a modification in function
> > I have to quit IPTHON console (in both with and with out qt console )
> > and reload the function freshly. If I need to see the changed I made in
> > the function. I tried below options
> 
> > del function name
> > import the module again  by issuing "from xxx.py import yy"
> 
> This doesn't re-import the module if xxx has already been imported. It
> simply rebinds xxx.yy to yy.
> 
> > import xxx.py
> 
> This also doesn't re-import the module if it has already been imported.
> 
> When you import a module, or a function from a module, a module object
> is created and stored in sys.modules. Any subsequent 'import <module>'
> calls will return a reference to that module object, and won't reload
> from file at all.
> 
> You can easily verify this by creating a test module 'foo' with a single
> line of `print('loading foo')` and then trying this from the console:
> 
>      In [1]: import foo
>      loading foo
> 
>      In [2]: del foo
> 
>      In [3]: import foo
> 
>      In [4]:
> 
> Note that you only see 'loading foo' the first time you import the
> module. In order to have the module loaded again rather than returning
> the existing reference, you would use `reload(foo)`:
> 
>      In [5]: reload(foo)
>      loading foo
> 
> So: in order to be able to use functions from a re-loaded module, you
> should always refer to them via the module object, and not import them
> directly:
> 
>      >>> import xxx
>      >>> xxx.yy() # original code
>      # ...modify function `yy` in your source file
>      >>> reload(xxx)
>      >>> xxx.yy() # new code
> 
> Or: you can reload the module and then rebind the functions:
> 
>      >>> from xxx import yy
>      >>> yy() # original code
>      # ...modify function `yy` in your source file
>      >>> reload(xxx)
>      >>> from xxx import yy
>      >>> yy() # new code
> 
> Hope this helps.
> 
> --

In Python 3 the reload built-in was moved to the imp module.
So use imp.reload(<module>) instead.


~Ramit



This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.  

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web