Reloading after from adder import * ????

John Machin sjmachin at lexicon.net
Sun Nov 19 00:34:03 EST 2006


Seymour wrote:
> 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[k]=x[k]
>         for k in y.keys(): new[k]=y[k]
>         return new
>
> At the interactive prompt I then said: from Adder import *
>
> After defining an instance of DictAdder as x=DictAdder() and trying to
> add two dictionaries, I notice a typo in DictAdder.  If I then go back
> and make a change to the DictAdder subclass I have to go through many
> steps to add two dictionaries (get the revision into active memory):
> 1. import Adder
> 2. Reload(Adder)
> 3. from Adder import *
> 4. x=DictAdder()
>
> Question: Is there an easier and quicker way to get DictAdder into
> active memory?????

This is one of the occasions where semicolons can come in handy in
Python:

import adder; reload(adder); from adder import *

Then depending on your OS and what add-on line editing kit (if any) you
are using, it should only a very small number of keystrokes to retrieve
that and execute it.

If you meant "is there a single command?", the answer is no, not AFAIK.

Having said that, you seem to have a strange idea about what classes
are for -- "self" is not used; having such a class "method" seems
pointless. All you need to do what you are doing  is a simple function,
whose body can be simply expressed in terms of dict methods:

def funnyfunc(x, y):
    """This is called a docstring; try to say what the func is doing
for you"""
    new = x.copy()
    new.update(y)
    return new

Of course that is probably not what you *want* to be doing; but you
might need to divulge what the Adder class is doing to get more help.

Hints: unlike some other languages,

(1) You don't need to have 1 module file per class; for several small
related classes (e.g. a main class and a handful of subclasses) it
makes a lot of sense to put it all in one module.

(2) To do most simple things, you don't even need to write a class at
all. For example, you can write a few hundred lines of clear
intelligible Python with very little must-recite-this-spell overhead to
do quite complicated data manipulations using only (a) built-in
functions (b) the methods of built-in types like strings, lists and
dictionaries (c) functions and classes in built-in modules (d) ditto
3rd-party modules. In Python classes usually are things that map onto
something in the real world or at least your model of that; you write
them when you need to; classes are not artificial hoops you must jump
through to do the equivalent of:
   print 2 + 2
You may end up using classes to emulate some other programming
constructs, but not yet.
Eventually you can write those 3rd party modules which are chock-full
of classes and other goodies, using them as a guide.

OK, relax, rant over :-)

HTH,
John




More information about the Python-list mailing list