473,698 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reloading modules and isinstance()

I am using a software system with an embedded Python interpreter
(version 2.3) for scripting. The KcsPoint2D.py module contains a
Point2D class with the following method:

def SetFromMidpoint (self, p1, p2):
if not isinstance(p1, Point2D) or not isinstance(p2, Point2D):
raise TypeError, 'some error message'
---

The problem is that after I launch my script once, and then use the
'Reload modules' button (presumably it calls reload() on all loaded
modules), the isinstance() tests shown above fails, even though the
expressions:

type(self)
type(p1)
type(p2)

all show: class 'KcsPoint2D.Poi nt2D'

Could you explain, please what is going on? Why the isinstance()
function returns False?
Dec 4 '07 #1
7 2732
Tlis wrote:
I am using a software system with an embedded Python interpreter
(version 2.3) for scripting. The KcsPoint2D.py module contains a
Point2D class with the following method:

def SetFromMidpoint (self, p1, p2):
if not isinstance(p1, Point2D) or not isinstance(p2, Point2D):
raise TypeError, 'some error message'
---

The problem is that after I launch my script once, and then use the
'Reload modules' button (presumably it calls reload() on all loaded
modules), the isinstance() tests shown above fails, even though the
expressions:

type(self)
type(p1)
type(p2)

all show: class 'KcsPoint2D.Poi nt2D'

Could you explain, please what is going on? Why the isinstance()
function returns False?
You just discovered one reason why reload() is a bad idea and IMHO shouldn't
be used at all - as tempting it might be.

Reload will re-execute the importing of the module and perform all necessary
initializations there, overwrite names in the module namespace and such.

But it _won't_ magically change classes or other objects being referred to
from other code that have been created prior to the reloading. Consider
this example:

class A(object):
foo = 10

a = A()

class A(object):
foo = 20

print a.foo
>>10
All that happens is that the name A is bound to a new class. So all
subsequent instantiations are of the new type. But not the old instances -
because the refer to their class not by NAME, but just have a pointer to
it.

Diez
Dec 4 '07 #2
On Tue, 04 Dec 2007 15:41:48 +0100, Diez B. Roggisch wrote:
You just discovered one reason why reload() is a bad idea and IMHO
shouldn't be used at all - as tempting it might be.

I disagree -- I find reload() extremely useful for interactively testing
modules. But I would never dream of using it in production code!
--
Steven.
Dec 5 '07 #3
Steven D'Aprano wrote:
On Tue, 04 Dec 2007 15:41:48 +0100, Diez B. Roggisch wrote:
>You just discovered one reason why reload() is a bad idea and IMHO
shouldn't be used at all - as tempting it might be.


I disagree -- I find reload() extremely useful for interactively testing
modules. But I would never dream of using it in production code!
I find short scripts I write & start on the commandline much more helpful -
and if I need interactivity, throwing in a

import pdb; pdb.set_trace()

or

python -i <script>

do what I want. But I certainly don't have time to hunt down bugs introduced
by reloading in the first place...

YMMV - and it seems it does :)

Diez
Dec 5 '07 #4
On 5 Dec, 13:18, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com .auwrote:
On Tue, 04 Dec 2007 15:41:48 +0100, Diez B. Roggisch wrote:
You just discovered one reason why reload() is a bad idea and IMHO
shouldn't be used at all - as tempting it might be.

I disagree -- I find reload() extremely useful for interactively testing
modules. But I would never dream of using it in production code!

--
Steven.
Please note, that I was using the 'Reload modules' functionality of
the software system in use, rather than the reload() function
directly. I admit, though, that in the background it just may call
reload() ...

With all the problems of the reload() function, I still hope, that
there should be possible to write a safe module 'reloader', that would
fix the references, as required (e.g. by changing the
variable.__clas s__ references). This should be provided by every
serious Python development environment.
Dec 5 '07 #5
On Dec 5, 2007 12:06 PM, Tlis <tl****@googlem ail.comwrote:
On 5 Dec, 13:18, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com .auwrote:
On Tue, 04 Dec 2007 15:41:48 +0100, Diez B. Roggisch wrote:
You just discovered one reason why reload() is a bad idea and IMHO
shouldn't be used at all - as tempting it might be.
I disagree -- I find reload() extremely useful for interactively testing
modules. But I would never dream of using it in production code!

--
Steven.

Please note, that I was using the 'Reload modules' functionality of
the software system in use, rather than the reload() function
directly. I admit, though, that in the background it just may call
reload() ...

With all the problems of the reload() function, I still hope, that
there should be possible to write a safe module 'reloader', that would
fix the references, as required (e.g. by changing the
variable.__clas s__ references). This should be provided by every
serious Python development environment.
It's very nice to say that until you actually think about what it
entails - it's an extraordinarily hard problem. I challenge you to
write one (you don't need to have much more than a beginner knowledge
of Python to start on one) and then come back with your experiences.
Dec 5 '07 #6
En Wed, 05 Dec 2007 15:06:43 -0300, Tlis <tl****@googlem ail.comescribiï ¿½:
With all the problems of the reload() function, I still hope, that
there should be possible to write a safe module 'reloader', that would
fix the references, as required (e.g. by changing the
variable.__clas s__ references). This should be provided by every
serious Python development environment.
Unfortunately that's not so simple, given the dynamic nature of Python.
With some help from the programmer, reload may perform better; by example,
if you never ever do "from module import xxx", and always do "import
module" and use module.xxx everywhere. But it gets rather annoying, and
still has many problems.

--
Gabriel Genellina

Dec 5 '07 #7
Tlis schrieb:
On 5 Dec, 13:18, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com .auwrote:
>On Tue, 04 Dec 2007 15:41:48 +0100, Diez B. Roggisch wrote:
>>You just discovered one reason why reload() is a bad idea and IMHO
shouldn't be used at all - as tempting it might be.
I disagree -- I find reload() extremely useful for interactively testing
modules. But I would never dream of using it in production code!

--
Steven.

Please note, that I was using the 'Reload modules' functionality of
the software system in use, rather than the reload() function
directly. I admit, though, that in the background it just may call
reload() ...

With all the problems of the reload() function, I still hope, that
there should be possible to write a safe module 'reloader', that would
fix the references, as required (e.g. by changing the
variable.__clas s__ references). This should be provided by every
serious Python development environment.
Wishful thinking. If I do

foo = {}
foo['some_key'] = somemodule.Some Class

somewhere in my code, how do you suppose is reload(somemodu le) to know
where in all the world I keep references to that class? Thus it would
essentially have to scan all references to anything, all list contents,
all class-properties, all everything. Which is not feasible.

reload() can be convenient, if you know its limitiations. But it has
some, and they are deeply rooted in the way python works. So you gonna
have to live with it.
Diez
Dec 6 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2467
by: Andy Jewell | last post by:
Does anyone know of a way to dynamically reload all the imported modules of a client module? I'm writing a program that I have broken down into quite a few submodules, and the 'configuration' is done with modules too. As I am developing the app, I need to test bits and pieces here and there. This is what I currently do: In each module, I have a section, just after the main imports like so:
0
1317
by: OKB (not okblacke) | last post by:
I'm fooling around with some MUD server code, and I want to add a "reload" command that will let MUD wizards reload the server modules, so that changes to the MUD parser and such can be effected without having to shut down and restart the server. Now, what I want to do at the top of my main module, instead of just "import somemodule", is something like this: try: reload(somemodule)
4
1357
by: Darren Dale | last post by:
If I am working interactively with 10 variables and wanted to pickle them and reload them tomorrow using the same variable names, would there be a direct way of doing it? Thanks, Darren
2
1661
by: aurora | last post by:
I am looking for a way for reloading updated modules in a long running server. I'm not too concerned about cascaded reload or objects already created. Just need to reload module xxx if the corresponding xxx.py is updated. I found something useful in module.__file__. Would it work if I use it to generate filenames xxx.py xxx.pyc and then compare their mtime? I'm not too sure about the mechanism of generation of .pyc file. Would it be too...
2
1308
by: Lowell Kirsh | last post by:
I have a driver module as well as several other modules. I am running the driver interactively from emacs - that is, I don't restart python on each run. I want to work it such that every time a modify the source for one of the non-driver modules and re-run the driver, the other modules will be reloaded. I have the following at the top of my driver module: import dbtest, util for module in : if module in sys.modules.keys():...
7
2072
by: Jorgen Grahn | last post by:
I have a set of tests in different modules: test_foo.py, test_bar.py and so on. All of these use the simplest possible internal layout: a number of classes containing test*() methods, and the good old lines at the end: if __name__ == "__main__": unittest.main() This is great, because each of the modules are runnable, and I can select classes or tests to run on the commandline if I want to. However, running all the tests from e.g. a...
12
1845
by: codefire | last post by:
Hi, I'm using the isinstance built-in function. I've found the docs for it, but there are no docs on the supported types. For example isinstance(a, int) works fine but isinstance(s, string) doesn't - because 'string is not known'. I do know how to import the types module and then use defined types such as 'types.StringType' - but the documentation says that from 2.2
3
1531
by: Seymour | last post by:
I created a module with the DictAdder subclass as follows: class DictAdder(Adder): def add(self, x, y): new={} for k in x.keys(): new=x for k in y.keys(): new=y return new At the interactive prompt I then said: from Adder import *
4
282
by: aine_canby | last post by:
I'm using python.exe to execute my modules. I have a music.py module which contains my classes and a main.py module which uses these classes. In python.exe, I call "import main" to execute my program. The problem is that I have to close python and reopen it everytime i change music.py or main.py. What should I be doing. Thanks, Aine.
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7721
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.