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

Discussion in 'Python' started by Ian, Sep 5, 2003.

  1. Ian

    Ian Guest

    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)
    Ian, Sep 5, 2003
    #1
    1. Advertising

  2. Ian

    Peter Otten Guest

    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
    Peter Otten, Sep 6, 2003
    #2
    1. Advertising

  3. Ian

    Ulrich Petri Guest

    "Ian" <> schrieb im Newsbeitrag
    news:...
    >
    >
    > 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
    Ulrich Petri, Sep 6, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Burger
    Replies:
    3
    Views:
    546
    Roland
    Jun 30, 2005
  2. John Salerno

    utility functions within a class?

    John Salerno, May 8, 2006, in forum: Python
    Replies:
    23
    Views:
    639
    John Salerno
    May 9, 2006
  3. aidy
    Replies:
    5
    Views:
    226
    Marcin Mielżyński
    Jun 4, 2006
  4. Glenn
    Replies:
    4
    Views:
    127
    Brian Candler
    Jan 28, 2009
  5. Paul Roche
    Replies:
    3
    Views:
    122
    Jeremy Bopp
    Oct 6, 2010
Loading...

Share This Page