Confusion About Classes

flamesrock flamesrock at gmail.com
Tue Dec 28 01:35:31 CET 2004


Hi,

I've been playing like mad with all sorts of python modules..but I
still can't seem to get my head around the proper use of a class and
self. The question stems from this code I made(snippet):
--------------------------------------------------------------------------------------
import httplib, ftplib, urllib2, exPyCrypto, score_configurations
#custom

class create_server:
def __init__(self, domain, servername, master_ftpUSER,
master_ftpPASS, score_ftpUSER, score_ftpPASS, httpport=None,
ftpport=None):
self.domain             = domain
if httpport is None:
self.__httpport     = '80'
else:
self.__httpport     = httpport
if ftpport is None:
self.__ftpport      = '21'
else:
self.__ftpport      = ftpport
if servername is None:
self.__servername   = 'SCORE-SERVER'
else:
self.__servername   = servername
self.master_ftpUSER     = master_ftpUSER
self.master_ftpPASS     = master_ftpPASS
self.score_ftpUSER      = score_ftpUSER
self.score_ftpPASS      = score_ftpPASS
self.parser             = ConfigParser.ConfigParser()

def createUniversalConfig(self, score_domain, score_servername,
score_port):
''''''
self.parser.add_section('score')
self.parser.set('score', 'domain', self.score_domain)
self.parser.set('score', 'server', self.score_servername)
self.parser.set('score', 'server', self.score_port)
-------------------------------------------------------------------------------------

The goal is to create a create_server object with the given parameters,
and then call the method createUniversalConfig() without passing and
parameters to it. So here are my questions:
1) in the function parameter heading <<def createUniversalConfig(self,
score_domain, score_servername, score_port):>> do I need to include
those parameters as written, like this-- <<def
createUniversalConfig(self, self.score_domain, self.score_servername,
self.score_port):>> or not pass any parameters at all?
2) Do I reference the body of the function like I originally wrote it:
self.parser.add_section('score')
self.parser.set('score', 'domain', self.score_domain)
self.parser.set('score', 'server', self.score_servername)
self.parser.set('score', 'server', self.score_port)
or, like this
self.parser.add_section('score')
self.parser.set('score', 'domain', score_domain)
self.parser.set('score', 'server', score_servername)
self.parser.set('score', 'server', score_port)
(if the goal is to call the method from the object without any
parameters)
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\




More information about the Python-list mailing list