Defining a code object from a function

Neil Benn neil.benn at cambridgeantibody.com
Fri Apr 6 05:10:22 EDT 2001


Hello,


                Thanks for that guys, I think I've found my problem.  I'm
actually using Jython cos I want to embed scripts into Java.  I've tested
out the examples you gave me and:-

    The examples work in a Python console but not in a Jython console.  Does
anyone know if the inspect module works in Jython?

    I know I could probably use Java's reflection to interpret the classes
made by Jython but this causes problem for cross scripting language
capability (I'm using BSF to allow different scripting languages to be
used).

    Once again, thank you kindly people!

Cheers,

Neil

"Jeremy Hylton" <jeremy at digicool.com> wrote in message
news:mailman.986499131.25725.python-list at python.org...
> >>>>> "NB" == Neil Benn <neil.benn at cambridgeantibody.com> writes:
>
>   NB> Hello,
>   NB>             I have a class as follows:-
>
>   NB> class plate:
>
>   NB>     NoColumns = 0
>   NB>     NoRows = 0
>
>   NB>     currentRow = 0
>
>   NB>     def __init__(self):
>   NB>         print "creating plate"
>   NB>         print "created plate"
>
>   NB>     def hasMoreRows(self):
>   NB>         if currentRow < noRows:
>   NB>             return 1ist
>   NB>         else:
>   NB>             return 0
>
>   NB>     def nextRow(self):
>   NB>         self.currentRow = self.currentRow + 1
>   NB>         return self.currentRow
>
>   NB>     I'm using the inspect module to get information about the
>   NB>     method
>   NB> arguments back.
>
>   NB> However...................
>
>   NB> My problem is [...]
>
> It looks like you have other problems :-).  The hasMoreRows() method
> will fail with a NameError because you use a bunch of unbound names.
> I think you need to change it to:
>
>     def hasMoreRows(self):
>         if self.currentRow < self.noRows:
>             return 1
>         else:
>             return 0
>
> You're also initializing NoColumns, NoRows, and currentRow as class
> attributes.  This will probably work correctly in your application,
> because all of the attributes you are using are immutable objects.
> The first assignment to, e.g. self.NoRows, will create an instance
> attribute self.NoRows.  But if you used a list or some other mutable
> object, you'd have to be more careful.  It is sometime better to
> initialize instance variable in the __init__():
>
>     def __init__(self):
>         self.currentRow = 0
>         self.NoRows = 0
>         self.NoColumns = 0
>         self.listAttr = []
>
> If you are clear on the distinction between class attributes and
> instance attributes, then I apologize for being pedantic.  But it's
> easy to get confused and the hasMoreRows() method suggests that you're
> new to Python.
>
> Jeremy
>
>
>
>





More information about the Python-list mailing list