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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ian

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

    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(ideall y 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\Li b\site-packages\Python win\pywin\frame work\scriptutil s.py",
    line 301, in RunScript
    exec codeObject in __main__.__dict __
    File "X:\ibutt_1\EEU \development\pr ototype\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)
  • Peter Otten

    #2
    Re: Help with a utility class and having a method call another method from within

    Ian wrote:
    [color=blue]
    > 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.[/color]

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

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

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

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

    def _privateByConve ntion(self):
    pass

    and then call it with a class or instance:

    Sample.test("fr om class")
    instance = Sample()
    instance.test(" from instance")
    instance.indire ct("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

    Comment

    • Ulrich Petri

      #3
      Re: Help with a utility class and having a method call another method from within


      "Ian" <2ianbutt@roger s.com> schrieb im Newsbeitrag
      news:37500ed8.0 309051159.1eef4 2c5@posting.goo gle.com...[color=blue]
      >
      >
      > 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)[/color]

      How about:

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

      HTH

      Cu Ulrich


      Comment

      Working...