[Tutor] Syntax when using classes question.

Luke Paireepinart rabidpoobear at gmail.com
Sun Nov 12 01:28:12 CET 2006


Chris Hengge wrote:
> I guess I'm just lost as to the point of classes... Outside of using 
> them to abstract a collection of methods that have similair roles I 
> dont see the point.. But when I group similar methods together 
> (promptForge is a bad example, but I've got class fileForge which has 
> a few file writing or reading methods), people tell me its overkill 
> (basicaly) =P 
Have you tried reading Alan Gauld's tutorial on classes?  I'm sure he 
explains their purpose quite well.

Classes can be used to group similar methods together, but so can 
modules, and that's usually what people do.
the point in using classes is when you have similar functions all 
operating on a single set of data.
that's the key.
If you find yourself calling the functions and passing in arguments, 
you're probably doing it wrong :)

Something like this

#proc.py
class ProcessFile(object):
    def input_file(self):
       f = file(filename)
       self.text = f.readlines()
       f.close()
      
    def __init__(self,filename):
       self.filename = filename
       input_file()

    def display_filename_and_contents(self):
       print "Filename: %s" % self.filename
       print "Contents:"
       for line in self.text:
          print line.strip()
       print "END.\n\n"
    def change_file(self,filename):
       self.filename = filename
       input_file()

#input1.txt
I am a cat.
What is the plural of Syllabus?
Can the penguin wear the hat, mommy, please?
That walrus looks absurdly skinny.
It weighs 800 pounds!
That's not so much.... for a walrus.

#input2.txt
What... is the air-speed velocity of an unladen swallow?
African or European?

#<(-_- )----> and so endeth the example codeth.

Now you can easily do something like this (in proc.py as well)

myfile = ProcessFile('input1.txt')
myfile.display_filename_and_contents()
myfile.change_file('input2.txt')
myfile.display_filename_and_contents()

will give you an output like this:

Filename: input1.txt
Contents:
I am a cat.
What is the plural of Syllabus?
Can the penguin wear the hat, mommy, please?
That walrus looks absurdly skinny.
It weighs 800 pounds!
That's not so much.... for a walrus.
END.


Filename: input2.txt
Contents:
What... is the air-speed velocity of an unladen swallow?
African or European?
END.


See, the main idea here is that, because these functions operate on the 
same dataset,
you give them all equal access to the data by putting them in a class 
and making the data attributes of the class.
For example, view the input_file function.
It doesn't need you to tell it what the filename is, because it gets it 
from the instance variable
self.filename
It doesn't need to return a value because it sticks the inputted text 
into self.text()

Now a self.text() file exists that contains the text of the file.
If you weren't doing this in a class,
and you wanted multiple functions to act upon the contents of a file, 
you'd do something like
f = file('input1.txt')
text = f.readlines()
f.close()
function1(text)
function2(text)
function3(text)
etc.
Now, if these were all in a class, you could just have an instance variable
self.text
that contains the data in the class,
and you wouldn't have to pass this to every function.


HTH,
-Luke

      




More information about the Tutor mailing list