How to call module functions inside class instance functions?

Discussion in 'Python' started by beginner, Aug 19, 2007.

  1. beginner

    beginner Guest

    Hi Everyone,

    I have encountered a small problems. How to call module functions
    inside class instance functions? For example, calling func1 in func2
    resulted in a compiling error.

    "my module here"

    def func1():
    print "hello"

    class MyClass:
    def func2():
    #how can I call func1 here.
    func1() #results in an error


    Thanks,
    Geoffrey
    beginner, Aug 19, 2007
    #1
    1. Advertising

  2. beginner

    Zentrader Guest

    On Aug 18, 5:40 pm, beginner <> wrote:
    > Hi Everyone,
    >
    > I have encountered a small problems. How to call module functions
    > inside class instance functions? For example, calling func1 in func2
    > resulted in a compiling error.
    >
    > "my module here"
    >
    > def func1():
    > print "hello"
    >
    > class MyClass:
    > def func2():
    > #how can I call func1 here.
    > func1() #results in an error
    >
    > Thanks,
    > Geoffrey


    You might want to check one of the online tutorials about how to code
    classes. Google or look at "Learning Python" here http://www.python-eggs.org/
    def func1():
    print "hello"

    class MyClass:
    def func2(self):
    #how can I call func1 here.
    func1() #results in an error

    MC= MyClass()
    MC.func2()
    Zentrader, Aug 19, 2007
    #2
    1. Advertising

  3. beginner

    Steve Holden Guest

    beginner wrote:
    > Hi Everyone,
    >
    > I have encountered a small problems. How to call module functions
    > inside class instance functions? For example, calling func1 in func2
    > resulted in a compiling error.
    >
    > "my module here"
    >
    > def func1():
    > print "hello"
    >
    > class MyClass:
    > def func2():
    > #how can I call func1 here.
    > func1() #results in an error
    >

    If you had bothered to include the error message it would have been
    obvious that the problem with your code isn't in body of the method at
    all - you have failed to include an argument to the method to pick up
    the instance on which the method is called. I am guessing that when you
    create an instance and call its func2 method you see the message

    Traceback (most recent call last):
    File "test07.py", line 12, in <module>
    myInstance.func2()
    TypeError: func2() takes no arguments (1 given)

    which would have been a very useful clue. Please include the traceback
    in future! Here's a version of your program that works.

    sholden@bigboy ~/Projects/Python
    $ cat test07.py
    "my module here"

    def func1():
    print "hello"

    class MyClass:
    def func2(self):
    #how can I call func1 here.
    func1() #results in an error

    myInstance = MyClass()
    myInstance.func2()

    sholden@bigboy ~/Projects/Python
    $ python test07.py
    hello

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
    Steve Holden, Aug 19, 2007
    #3
  4. beginner <> wrote:
    > I have encountered a small problems. How to call module functions
    > inside class instance functions? For example, calling func1 in func2
    > resulted in a compiling error.
    >
    > "my module here"
    >
    > def func1():
    > print "hello"
    >
    > class MyClass:
    > def func2():
    > #how can I call func1 here.
    > func1() #results in an error


    rhymes@groove ~ % cat t.py
    def func1():
    print "hello"

    class MyClass:
    def func2():
    func1()
    rhymes@groove ~ % python -c "import t"
    rhymes@groove ~ %

    As you can see there no compiling error, because the syntax is correct,
    you'll eventually get a runtime error like this:

    >>> import t
    >>> c = t.MyClass()
    >>> c.func2()

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: func2() takes no arguments (1 given)

    That's because you left out the "self" argument in the definition of
    "func2()". This version is correct:

    --
    def func1():
    print "hello"

    class MyClass(object):
    def func2(self):
    func1()

    c = MyClass()
    c.func2()
    --

    rhymes@groove ~ % python tcorrect.py
    hello


    HTH

    --
    Lawrence, oluyede.org - neropercaso.it
    "It is difficult to get a man to understand
    something when his salary depends on not
    understanding it" - Upton Sinclair
    Lawrence Oluyede, Aug 19, 2007
    #4
  5. beginner

    beginner Guest

    On Aug 18, 8:18 pm, Steve Holden <> wrote:
    > beginner wrote:
    > > Hi Everyone,

    >
    > > I have encountered a small problems. How to call module functions
    > > inside class instance functions? For example, calling func1 in func2
    > > resulted in a compiling error.

    >
    > > "my module here"

    >
    > > def func1():
    > > print "hello"

    >
    > > class MyClass:
    > > def func2():
    > > #how can I call func1 here.
    > > func1() #results in an error

    >
    > If you had bothered to include the error message it would have been
    > obvious that the problem with your code isn't in body of the method at
    > all - you have failed to include an argument to the method to pick up
    > the instance on which the method is called. I am guessing that when you
    > create an instance and call its func2 method you see the message
    >
    > Traceback (most recent call last):
    > File "test07.py", line 12, in <module>
    > myInstance.func2()
    > TypeError: func2() takes no arguments (1 given)
    >
    > which would have been a very useful clue. Please include the traceback
    > in future! Here's a version of your program that works.
    >
    > sholden@bigboy ~/Projects/Python
    > $ cat test07.py
    > "my module here"
    >
    > def func1():
    > print "hello"
    >
    > class MyClass:
    > def func2(self):
    > #how can I call func1 here.
    > func1() #results in an error
    >
    > myInstance = MyClass()
    > myInstance.func2()
    >
    > sholden@bigboy ~/Projects/Python
    > $ python test07.py
    > hello
    >
    > regards
    > Steve
    > --
    > Steve Holden +1 571 484 6266 +1 800 494 3119
    > Holden Web LLC/Ltd http://www.holdenweb.com
    > Skype: holdenweb http://del.icio.us/steve.holden
    > --------------- Asciimercial ------------------
    > Get on the web: Blog, lens and tag the Internet
    > Many services currently offer free registration
    > ----------- Thank You for Reading -------------- Hide quoted text -
    >
    > - Show quoted text -


    I apologize for not posting the exact code and error message. The
    missing "self" is due to a typo of mine. It is not really the problem
    I am encountering.

    testmodule.py
    -----------------
    """Test Module"""

    def __module_level_func():
    print "Hello"

    class TestClass:
    def class_level_func(self):
    __module_level_func()


    main.py
    ------------------
    import testmodule

    x=testmodule.TestClass()
    x.class_level_func()


    The error message I am encountering is: NameError: global name
    '_TestClass__module_level_func' is not defined

    I think it has something to do with the two underscores for
    __module_level_func. Maybe it has something to do with the python
    implementation of the private class level functions.

    By the way, the reason I am naming it __module_level_func() is because
    I'd like __module_level_func() to be private to the module, like the C
    static function. If the interpreter cannot really enforce it, at least
    it is some sort of naming convention for me.

    Thanks,
    Geoffrey
    beginner, Aug 19, 2007
    #5
  6. beginner

    beginner Guest

    On Aug 18, 8:13 pm, Zentrader <> wrote:
    > On Aug 18, 5:40 pm, beginner <> wrote:
    >
    >
    >
    >
    >
    > > Hi Everyone,

    >
    > > I have encountered a small problems. How to call module functions
    > > inside class instance functions? For example, calling func1 in func2
    > > resulted in a compiling error.

    >
    > > "my module here"

    >
    > > def func1():
    > > print "hello"

    >
    > > class MyClass:
    > > def func2():
    > > #how can I call func1 here.
    > > func1() #results in an error

    >
    > > Thanks,
    > > Geoffrey

    >
    > You might want to check one of the online tutorials about how to code
    > classes. Google or look at "Learning Python" herehttp://www.python-eggs.org/
    > def func1():
    > print "hello"
    >
    > class MyClass:
    > def func2(self):
    > #how can I call func1 here.
    > func1() #results in an error
    >
    > MC= MyClass()
    > MC.func2()- Hide quoted text -
    >
    > - Show quoted text -


    Thanks for your help. The missing "self" is a typo of mine. It is not
    the problem I am encountering. Sorry for posting the wrong code.
    beginner, Aug 19, 2007
    #6
  7. beginner

    Zentrader Guest

    > By the way, the reason I am naming it __module_level_func() is because
    > I'd like __module_level_func() to be private to the module, like the C
    > static function. If the interpreter cannot really enforce it, at least
    > it is some sort of naming convention for me.


    re the above: set file permissions for testmodule.py to limit access.
    IMHO it is a better solution.
    Zentrader, Aug 19, 2007
    #7
  8. beginner <> wrote:
    ...
    > testmodule.py
    > -----------------
    > """Test Module"""
    >
    > def __module_level_func():
    > print "Hello"
    >
    > class TestClass:
    > def class_level_func(self):
    > __module_level_func()
    >
    >
    > main.py
    > ------------------
    > import testmodule
    >
    > x=testmodule.TestClass()
    > x.class_level_func()
    >
    >
    > The error message I am encountering is: NameError: global name
    > '_TestClass__module_level_func' is not defined
    >
    > I think it has something to do with the two underscores for
    > __module_level_func. Maybe it has something to do with the python
    > implementation of the private class level functions.
    >
    > By the way, the reason I am naming it __module_level_func() is because
    > I'd like __module_level_func() to be private to the module, like the C
    > static function. If the interpreter cannot really enforce it, at least
    > it is some sort of naming convention for me.


    The two underscores are exactly the cause of your problem: as you see in
    the error message, the compiled has inserted the CLASS name (not MODULE
    name) implicitly there. This "name mangling" is part of Python's rules.

    Use a SINGLE leading underscore (NOT double ones) as the "sort of naming
    convention" to indicate privacy, and Python will support you (mostly by
    social convention, but a little bit technically, too); use a different
    convention (particularly one that fights against the language rules;-)
    and you're "fighting city hall" to no good purpose and without much hope
    of achieving anything whatsoever thereby.


    Alex
    Alex Martelli, Aug 19, 2007
    #8
    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. Sridhar R
    Replies:
    14
    Views:
    1,400
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  2. Gerry Sutton
    Replies:
    1
    Views:
    538
    Peter Otten
    Apr 16, 2005
  3. Gerard Flanagan
    Replies:
    3
    Views:
    447
    Terry Hancock
    Nov 19, 2005
  4. gentlestone
    Replies:
    6
    Views:
    945
    Bruno Desthuilliers
    Oct 6, 2009
  5. Martin P. Hellwig
    Replies:
    1
    Views:
    374
    Martin P. Hellwig
    Mar 26, 2010
Loading...

Share This Page