[Tutor] Shortening the code of a finsihed program.

Steven D'Aprano steve at pearwood.info
Sat Nov 26 01:14:02 CET 2011


Hello Mic,

Mic wrote:
> 
>> Its usually better to paste long programs into a pastebin web site and
>> give us a link.
>> This saves cluttering up people mail with long messages, and also the
>> pastebin rendering will usually be easier to read with syntax coloring 
>> etc.

Please keep a attribution line when quoting people directly. Even 
something simple like "Fred wrote" will do.


> Alright. Sorry if I should know this, but what is a pastebin web site 
> and how do
> I paste my program into a pastebin web site?

Here's one: http://pastebin.com/

But as I've just written in another post, please don't use it :)


>> While its perfectly legal Python to create a class inside a method its
>> very unusual in practice and very restricting in the use of the class.
>> Its nearly always better to declare all your classes at the top level of
>> the program.
> 
> Why is it restricting?


Because generally speaking, if you define a class inside a function (or 
method), you can't use it *except* inside that function or method.

class K:
     def method(self, x):
         class Q:
             pass

instance = Q()


This doesn't work because Q is not visible from the outside of K.method. 
Sometimes this is what you want; generally it is not.


I don't know much about tkinter, so I will avoid commenting about your code.



-- 
Steven


More information about the Tutor mailing list