Re: [Tutor] is it legal to have a class within a def

2008-01-03 Thread Kent Johnson
johnf wrote:
> So I started thinking why would I need the class outside of the function.  If 
> I instead used "import class" would I get a performance improvement?  The 
> function creates an instance each time it is required and then releases and 
> closes.  
> 
> I am very interested in this possible difference between importing the class 
> vs using the inline class.

There is another possibility which is probably what I would do - just 
define the class at global scope in the same module that uses it. 
Instead of

def f():
   class Z(object):
 pass
   # do something with Z

write

def f():
   # do something with Z

class Z(object):
   pass


I.e. you don't have to put the class def'n in a separate module.
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is it legal to have a class within a def

2008-01-03 Thread johnf
On Thursday 03 January 2008 10:13:18 am Alan Gauld wrote:
> "johnf" <[EMAIL PROTECTED]> wrote
>
> >   If the user types in a partial of the key then the dialog
> > appears and the user picks from the list.  The details of the dialog
> > are
> > dynamic for each call (based on some meta data) of the showModal().
>
> This might be a valid case for defining the class in the function
> since
> it could be that the number of fields, the labels etc change depending
> on the input values. That is exactly the kind of place where a local
> class
> makes sense.
>
> > I am very interested in this possible difference between importing
> > the class
> > vs using the inline class.  Any thoughts as what the difference is
> > will help
> > me understand Python a little better.
>
> There is no great secret, its just that by putting the class
> definition
> into the function you have to execute the definition each time you
> execute the function. If you put the class in a module and import
> it then the class definition is executed at import time and thats it.
>
> But if you need to change the definition each time you instantiate the
> dialog you will have a very complex init method so you might find
> it easier to redefine the class each time instead.

Thanks for the response.  Having only a short period using python under my 
belt I find your type of help reassuring.

-- 
John Fabiani
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is it legal to have a class within a def

2008-01-03 Thread Alan Gauld
"johnf" <[EMAIL PROTECTED]> wrote

>   If the user types in a partial of the key then the dialog
> appears and the user picks from the list.  The details of the dialog 
> are
> dynamic for each call (based on some meta data) of the showModal().

This might be a valid case for defining the class in the function 
since
it could be that the number of fields, the labels etc change depending
on the input values. That is exactly the kind of place where a local 
class
makes sense.

> I am very interested in this possible difference between importing 
> the class
> vs using the inline class.  Any thoughts as what the difference is 
> will help
> me understand Python a little better.

There is no great secret, its just that by putting the class 
definition
into the function you have to execute the definition each time you
execute the function. If you put the class in a module and import
it then the class definition is executed at import time and thats it.

But if you need to change the definition each time you instantiate the
dialog you will have a very complex init method so you might find
it easier to redefine the class each time instead.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is it legal to have a class within a def

2008-01-03 Thread johnf
On Thursday 03 January 2008 12:22:25 am Alan Gauld wrote:
> Are you sure you made it a modal dialog?
> Any dialog will do that if it is opened modelessly, you need to
> use the modal version to make it block the app.

Yes.  I believe the way I have coded the dialog causes a bug.  If I create a 
static dialog and use showModal() it works as expected.
>
> Putting the class code in a functiion is legal but very inefficient
> and also prevents you from storing state etc in the dialog - you
> would need to reinitialise all valuues on each use. Thats OK if
> its simple but a lot of work(and slow)  if its complex.

I am creating a function that does a lookup of PK or FK fields.  If the user 
does type in the correct identifier then the dialog does not appear and is 
not created.  If the user types in a partial of the key then the dialog 
appears and the user picks from the list.  The details of the dialog are 
dynamic for each call (based on some meta data) of the showModal(). 

So I started thinking why would I need the class outside of the function.  If 
I instead used "import class" would I get a performance improvement?  The 
function creates an instance each time it is required and then releases and 
closes.  

I am very interested in this possible difference between importing the class 
vs using the inline class.  Any thoughts as what the difference is will help 
me understand Python a little better.

-- 
John Fabiani
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is it legal to have a class within a def

2008-01-03 Thread Alan Gauld

"johnf" <[EMAIL PROTECTED]> wrote

> 1 and 3 are my reasons.  I'm creating a Dabo app.  When I attempted 
> to create
> a special class that contained a dialog box I discovered that the 
> dialog
> class created an indepentant type of window and allowed my program 
> to
> continue running without waiting for the dialog to return a value 
> first.

Are you sure you made it a modal dialog?
Any dialog will do that if it is opened modelessly, you need to
use the modal version to make it block the app.

Putting the class code in a functiion is legal but very inefficient
and also prevents you from storing state etc in the dialog - you
would need to reinitialise all valuues on each use. Thats OK if
its simple but a lot of work(and slow)  if its complex.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is it legal to have a class within a def

2008-01-03 Thread Alan Gauld

"bob gailer" <[EMAIL PROTECTED]> wrote

> So the question becomes "why would you want to do that?"

The main reason I could think of was to create a factory 
method for dynamically creating classes based on input 
parameters - for example currency convertors or similar.
Equally you could change method implementations 
by passing lambdas etc.

Useful in certain other languages but much less so in 
Python which allows dynamic changes to classes/objects 
after creation anyway.

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is it legal to have a class within a def

2008-01-03 Thread Alan Gauld

"johnf" <[EMAIL PROTECTED]> wrote 

> def someMethod():
>   class MyClass(object):
>   .
>  if something:
> .
> return someval


Did you try it?

>>> def f():
...   class C: pass
...   return C
...
>>> def g(x):
...   class C: pass
...   if x == 42:
... return C
...   else: return 666
...
>>> c = f()
>>> c

>>>

It works for me...


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is it legal to have a class within a def

2008-01-02 Thread johnf
On Wednesday 02 January 2008 09:31:19 pm you wrote:
> johnf wrote:
> > def someMethod():
> >class MyClass(object):
> >.
> >   if something:
> >  .
> >  return someval
>
> Legal? Well the police won't come after you!
>
That's a good thing!
> Python allows a class statement anywhere. So this use is part of the
> language.
>
> So the question becomes "why would you want to do that?"
>
> Considerations:
>
> 1 - this usage creates a local object MyClass. The object will not be
> visible outside the function. The class definition is executed each time
> the function is called.
>
> 2 - the object could be returned or assigned to a global or added to a
> parameter that is a collection.
>
> 3 - if no instances are created in the function the object will
> disappear once the function execution ends.

1 and 3 are my reasons.  I'm creating a Dabo app.  When I attempted to create 
a special class that contained a dialog box I discovered that the dialog 
class created an indepentant type of window and allowed my program to 
continue running without waiting for the dialog to return a value first.  
However, I noticed if I created the dialog within a function my program 
stopped and waited until the dialog was closed to continue.   I think that I 
could define the Classes outside of the function and just import them when 
needed.  But then I wanted a self contained function so I asked why not place 
the class code in the function?  What is the difference?

-- 
John Fabiani
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is it legal to have a class within a def

2008-01-02 Thread bob gailer
johnf wrote:
> def someMethod():
>class MyClass(object):
>.
>   if something:
>  .
>  return someval
>   
>   
Legal? Well the police won't come after you!

Python allows a class statement anywhere. So this use is part of the 
language.

So the question becomes "why would you want to do that?"

Considerations:

1 - this usage creates a local object MyClass. The object will not be 
visible outside the function. The class definition is executed each time 
the function is called.

2 - the object could be returned or assigned to a global or added to a 
parameter that is a collection.

3 - if no instances are created in the function the object will 
disappear once the function execution ends.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor