Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Help with a utility class and having a method call another method from within

Reply
Thread Tools

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

 
 
Ian
Guest
Posts: n/a
 
      09-05-2003
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\scri pts\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)
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      09-06-2003
Ian wrote:

> 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

 
Reply With Quote
 
 
 
 
Ulrich Petri
Guest
Posts: n/a
 
      09-06-2003

"Ian" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) om...
>
>
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call a method within another method Paul Roche Ruby 3 10-06-2010 04:02 AM
Defining an ActiveRecord class within a method of another class Glenn Ruby 4 01-28-2009 07:06 PM
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
can call a method, but not a method within a user-defined class aidy Ruby 5 06-04-2006 08:10 PM
Utility class to check validity of package, class, and method name Martin Burger Java 3 06-30-2005 12:47 PM



Advertisments