calling the function of one class from another class

Furkan Kuru furkankuru at gmail.com
Sat Sep 22 06:28:40 EDT 2007


On 9/22/07, Mridula Ramesh <mridula.ccpl at gmail.com> wrote:
>
> hi.
>
> i currently have code structured like this:
>
> classA():
> >     def __init__():
> >      ..............
> >      ..............
> >
> >     def fnc1():
> >     ....................
> >     ....................
> >
> >
> > classB():
> >    def __init__():
> >     ........................
> >     ........................
> >     classA.fnc1()    #this is where i get an error
> >
>
> TypeError: unbound method fnc1() must be called with classA instance as
> first argument (got nothing instead)
>
> when i do  fnc1(classA) i get:
>
> NameError: global name 'fnc1' is not defined
>
> am i violating some programming rule by trying to call fnc1 in classB? i
> am only now learning OO alongside python, so i'm not sure! also, can someone
> please tell me where to go for more articles on the classes and functions
> and calling them from other places?
>
> thanks a lot!
>
> mridula.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

you should create an instance of ClassA:

a = ClassA()
a.fnc1()

or if you want a static function you should declare the method as static

classA():
    def __init__():
     ..............
     ..............
    @staticmethod
    def fnc1():
    ....................
    ....................
-- 
Furkan Kuru
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070922/b757cbb4/attachment.html>


More information about the Python-list mailing list