making a class callable

Discussion in 'Python' started by dude, Mar 4, 2011.

  1. dude

    dude Guest

    I've been struggling with getting my class to behave the way I want
    it.

    I have python module called ohYeah.py, defined as follows...
    #File Begin
    class foo:

    def __init__(self, arg1):
    print arg1
    self.ohYeah = arg1

    def whatwhat(self):
    return self.ohYeah
    #EOF

    My goal is to be able to instantiate the class foo from another python
    module, like so:

    # Example Usage
    f = foo("wow")
    j = foo("amazing")
    f.whatwhat()
    wow
    j.whatwhat()
    amazing
    #

    However, I always get the "module not callable" error. After entering
    a "def __call__" method in class foo, still get the same problem. Can
    someone please point me in the right direction for being able to
    achieve the Example Usage above? I'm sure there is something trivial
    I'm missing, but after digging around online for a day, I couldn't
    find the missing piece. Thanks in advance.
    dude, Mar 4, 2011
    #1
    1. Advertising

  2. dude

    MRAB Guest

    On 04/03/2011 01:45, dude wrote:
    > I've been struggling with getting my class to behave the way I want
    > it.
    >
    > I have python module called ohYeah.py, defined as follows...
    > #File Begin
    > class foo:
    >
    > def __init__(self, arg1):
    > print arg1
    > self.ohYeah = arg1
    >
    > def whatwhat(self):
    > return self.ohYeah
    > #EOF
    >
    > My goal is to be able to instantiate the class foo from another python
    > module, like so:
    >
    > # Example Usage
    > f = foo("wow")
    > j = foo("amazing")
    > f.whatwhat()
    > wow
    > j.whatwhat()
    > amazing
    > #
    >
    > However, I always get the "module not callable" error. After entering
    > a "def __call__" method in class foo, still get the same problem. Can
    > someone please point me in the right direction for being able to
    > achieve the Example Usage above? I'm sure there is something trivial
    > I'm missing, but after digging around online for a day, I couldn't
    > find the missing piece. Thanks in advance.


    How are you importing it?

    It should be something like:

    from ohYeah import foo

    BTW, the recommendation is for class names to be CamelCase and modules
    names to be lowercase.
    MRAB, Mar 4, 2011
    #2
    1. Advertising

  3. dude

    dude Guest

    On Mar 3, 6:07 pm, MRAB <> wrote:
    > On 04/03/2011 01:45, dude wrote:
    >
    >
    >
    > > I've been struggling with getting my class to behave the way I want
    > > it.

    >
    > > I have python module called ohYeah.py, defined as follows...
    > > #File Begin
    > > class foo:

    >
    > >      def __init__(self, arg1):
    > >          print arg1
    > >          self.ohYeah = arg1

    >
    > >      def whatwhat(self):
    > >          return self.ohYeah
    > > #EOF

    >
    > > My goal is to be able to instantiate the class foo from another python
    > > module, like so:

    >
    > > # Example Usage
    > > f = foo("wow")
    > > j = foo("amazing")
    > > f.whatwhat()
    > > wow
    > > j.whatwhat()
    > > amazing
    > > #

    >
    > > However, I always get the "module not callable" error.  After entering
    > > a "def __call__" method in class foo, still get the same problem.  Can
    > > someone please point me in the right direction for being able to
    > > achieve the Example Usage above?  I'm sure there is something trivial
    > > I'm missing, but after digging around online for a day, I couldn't
    > > find the missing piece.  Thanks in advance.

    >
    > How are you importing it?
    >
    > It should be something like:
    >
    >      from ohYeah import foo
    >
    > BTW, the recommendation is for class names to be CamelCase and modules
    > names to be lowercase.


    That was the problem. I was using:
    import ohYeah

    Thanks!
    dude, Mar 4, 2011
    #3
  4. dude

    Tom Zych Guest

    dude wrote:
    >>> f = foo("wow")

    ....
    >>> However, I always get the "module not callable" error.

    ....
    > That was the problem. I was using:
    > import ohYeah


    To get that error, I think you must have been importing
    a module named "foo" as well. Or you would have gotten
    a NameError instead.

    --
    Tom Zych /
    Q: I'm having problems with my Windows software. Will you help me?
    A: Yes. Go to a DOS prompt and type "format c:". Any problems you are
    experiencing will cease within a few minutes. -- Hacker Howto
    Tom Zych, Mar 4, 2011
    #4
    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. Nathan Duran
    Replies:
    1
    Views:
    323
    Arnaud Delobelle
    Apr 10, 2008
  2. vbgunz
    Replies:
    21
    Views:
    601
  3. exiquio
    Replies:
    2
    Views:
    514
    exiquio
    Oct 7, 2008
  4. plovet
    Replies:
    0
    Views:
    553
    plovet
    Jan 17, 2011
  5. Ulrich Eckhardt
    Replies:
    6
    Views:
    90
    Peter Otten
    Jul 12, 2013
Loading...

Share This Page