[Tutor] Class understanding

Che M pine508 at hotmail.com
Tue Nov 24 18:25:27 CET 2009




> 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







 		 	   		  
_________________________________________________________________
Windows 7: It works the way you want. Learn more.
http://www.microsoft.com/Windows/windows-7/default.aspx?ocid=PID24727::T:WLMTAGL:ON:WL:en-US:WWL_WIN_evergreen:112009v2
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091124/4a6d72e4/attachment.htm>


More information about the Tutor mailing list