Help with a utility class and having a method call another method from within

I

Ian

I have written this utility class that has a method which calls
another method. I don't want to pass in the self parameter and was
wondering by doing this does it make the function to be private or
uncallable outside of the class.

This code below works, but I didn't really want to pass self into
testBob1(ideally having it as private), I wanted the declaration to be
def testBob1(a). Can someone explains to me why this can't be done ?
I would get the following error:

Traceback (most recent call last):
File "C:\Python22\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 301, in RunScript
exec codeObject in __main__.__dict__
File "X:\ibutt_1\EEU\development\prototype\martini\scripts\aaa.py",
line 3, in ?
print SampleClassd().testBob2(2)
File "SampleClass.py", line 15, in testBob2
return SampleClassd().testBob1(a)
TypeError: testBob1() takes exactly 1 argument (2 given


in file-->SampleClass.py

class SampleClassd :

def __init__(self) :
self.test = 'a'

def testBob1(self, a) :
if a == 1:
return True
else:
return False

def testBob2(self, a) :
return SampleClassd().testBob1(a)

Main file
=========

from SampleClass import SampleClassd

print SampleClassd().testBob2(2)
 
P

Peter Otten

Ian said:
I have written this utility class that has a method which calls
another method. I don't want to pass in the self parameter and was
wondering by doing this does it make the function to be private or
uncallable outside of the class.

For new style classes, you can write methods without a self parameter:

class Sample(object):
def test(arg):
print arg
test = staticmethod(test) # this is crucial

def indirect(self, arg):
Sample.test("indirect(%s)" % arg)

def __mangled(self):
# transformed into _Sample__mangled by the compiler
pass

def _privateByConvention(self):
pass

and then call it with a class or instance:

Sample.test("from class")
instance = Sample()
instance.test("from instance")
instance.indirect("from instance")

In Python, privacy is rather a gentlemen's agreement. You can start a method
name with a single underscore "_" to signal "use at your own risk".
Two underscores trigger some name mangling by the compiler and can be used
to avoid name clashes. Occasions where this is useful, e. g. to protect a
method against overriding in a subclass, should be rare.

Peter
 
U

Ulrich Petri

Ian said:
in file-->SampleClass.py

class SampleClassd :

def __init__(self) :
self.test = 'a'

def testBob1(self, a) :
if a == 1:
return True
else:
return False

def testBob2(self, a) :
return SampleClassd().testBob1(a)

How about:

def testBob2(self,a):
return self.testBob1(a)

HTH

Cu Ulrich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top