473,406 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.Point2D'

Could you explain, please what is going on? Why the isinstance()
function returns False?
Dec 4 '07 #1
7 2711
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.Point2D'

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.__class__ references). This should be provided by every
serious Python development environment.
Dec 5 '07 #5
On Dec 5, 2007 12:06 PM, Tlis <tl****@googlemail.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.__class__ 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****@googlemail.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.__class__ 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.__class__ references). This should be provided by every
serious Python development environment.
Wishful thinking. If I do

foo = {}
foo['some_key'] = somemodule.SomeClass

somewhere in my code, how do you suppose is reload(somemodule) 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
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'...
0
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...
4
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
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...
2
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...
7
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...
12
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)...
3
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.