Confusion About Classes

M.E.Farmer mefjr75 at hotmail.com
Tue Dec 28 00:56:48 EST 2004


flamesrock wrote:
[snip]
> 3) Can I create a configParser object and just call self.parser.set()
> inside the function without passing it as a parameter, or do I pass
it
> as a parameter and call it as parser.set (without the 'self'
> statement.)
>
> Basically..I'm confused on the proper use of 'self' in this type of
> situation, even after going through all these different tutorials.
Any
> ideas?
>
> -thanks in advance\
Ok you have several problems that I see.
First you are having namespace difficulties.
I am not sure you 'get it' yet , but if you stay around long enough you
will ;)
http://docs.python.org/tut/node11.html

The proper use of 'self' in a class is its namespace.
You *have* to pass 'self' to all member functions of a class sp the all
can have a reference to the namespace.
It makes it easy to pass data back and forth between functions in a
class.
Below is an example of functions calling functions within a class and
setting attributes, maybe this will clear it up some
example:
py>class bla:
...    def __init__(self):
...        self.data=0
...    def setData(self, data):
...        self.data = data
...    def getData(self):
...        return self.data
...    def nullData(self):
...        self.setData(None)

Namespace is one of those crucial concepts you *must* master in python.
Do a search on google for python namespace, and read, and code, till
you get it. Master namespace if you have not already. Break out an
interpreter and hack away. Use dir() around your classes and functions
and 'see' for yourself what is in them. Do a search on this newsgroup
for namespace. This has been explained much better than I can.

Second if you haven't done it yet read up on configparser.
http://docs.python.org/lib/module-ConfigParser.html
before implementing a universalconfigwriter/reader you need to know how
to find previously written sections and options , and add error
checking etc..
Especially note this page.
(Hint this will save you from many long boring hours reinventing all
this junk and avoiding some eval hack to get the stuff back to the
right type.)
If you think I am kidding note others in this newsgroup have been there
done that, not me of course, I never need documentation ;)
Hth,
M.E.Farmer




More information about the Python-list mailing list