[Tutor] Class understanding

Dave Angel davea at ieee.org
Tue Nov 24 22:48:08 CET 2009



Che M wrote:
>
>   
>> Date: Tue, 24 Nov 2009 10:27:05 -0600
>> From: jammer10000 at gmail.com
>> To: tutor at python.org
>> Subject: [Tutor] Class understanding
>>
>> Hi all... Have been attempting to understand classes... Been getting
>> along without them for a while now and feel it's time to jump in....
>>
>> What I want to do it start a log with the logging module... I have
>> this working without classes, but want to try... Here is a snippet of
>> the code that I am hacking on:
>>     
>
> I'm sure the better explainers will jump in presently, but let me try
> a few tips...
>
>   
>> class logger():
>>     
>
> The convention in Python is to make class names capitalized.  It is
> not necessary, but it is a good habit to get into, so class Logger().
>
>   
>>                         import logging
>>     
>
> Imports are traditionally done at the top of a Python file, not within
> a class. 
>
>   
>> logger()
>>     
>
> This calls the class but doesn't create a name for an instance of
> the class, so you won't be able to access it later.  Instead, try
> (assuming you rename logger() to Logger() ),
>
> logger_instance = Logger()
>
> Now you have a name for that instance of the class, and so
> can access the goodies inside the class.  
>
>   
>> logger.write2log(log_info)
>>     
>
> So that would now be:
>
> logger_instance.write2log(log_info)
>
>   
>> encouragement, or pointers to good docs would be helpful... I've done
>> a lot of searching via Google on classes, and it's all confusing to
>> me...
>>     
>
> Keep trying.  There have to be tons of good tutorials on classes.
> They fall under the heading of "Object Oriented Programming".   I tend
> to think of a class as a "container" that has all the stuff you will need
> to do a certain set of actions.  It can contain data (facts) and it can 
> contain methods (functions).  You can create one or more "instances"
> of any class (a traditional example being that Dog() is a class whereas
> fluffy is an instance of a dog, and therefore has all the traditional dog
> methods, like bark(), wag(), etc.)
>
> CM
>
>
>
>   
For my first class, I'd have picked something self-contained, and 
probably something dumb & simple, so as not to be confused between the 
stuff in the imports and the problems in understanding how class 
instances, methods, and attributes work.  Anyway, you probably 
understand the logging module better than I;  you certainly couldn't 
understand less.

Also, probably because you used tabs, the current code is heavily 
indented, and pretty hard to follow.  The def line is indented about 26 
columns, where I'd expect four.

CM has pointed out several important things. 

In addition, I need to point out that you need a "self" parameter on 
your method(s). 

And that if you use the same name for the argument as you used in the 
parameter, you can get confused as to who is doing what. 

Also, you want to derive new classes from object, for reasons that 
probably won't matter now, but when they do, it's easier if you've 
already got the habit. 

And finally I don't think you were planning to return an empty tuple.  
Probably you used syntax from other languages.  In Python, to return 
nothing, use one of three forms:  1) fall off the end of the 
function/method  2) return with no argument 3) return None


So your code would become:


import logging

class Logger:
    ... some initialization logic, which I don't know about...
    def write2log(self, log_msg):
        print "writing to log", log_msg
        ... some logging stuff...
        return

inst = Logger()
log_info = "This is first msg"
inst.write2log(log_info)

I'm not sure why this is a class, unless you want to be able to have 
multiple loggers (instances of Logger).  And in that case, you 
presumably would need another method, the Python constructor, which is 
called __init__()


DaveA


More information about the Tutor mailing list