Jython callable. How?

Discussion in 'Python' started by Sirotin Roman, Mar 4, 2012.

  1. Hi.
    How exactly jython decides is object callable or not? I defined
    __call__ method but interpreter says it's still not callable.
    BTW, my code works in cpython
     
    Sirotin Roman, Mar 4, 2012
    #1
    1. Advertising

  2. On Sun, 04 Mar 2012 16:03:56 +0700, Sirotin Roman wrote:

    > Hi.
    > How exactly jython decides is object callable or not? I defined __call__
    > method but interpreter says it's still not callable. BTW, my code works
    > in cpython


    Works for me.

    steve@runes:~$ jython
    *sys-package-mgr*: processing modified jar, '/usr/share/java/servlet-
    api-2.5.jar'
    Jython 2.5.1+ (Release_2_5_1, Aug 4 2010, 07:18:19)
    [OpenJDK Client VM (Sun Microsystems Inc.)] on java1.6.0_18
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    >>> class Test:

    .... def __call__(self):
    .... return 42
    ....
    >>>
    >>> x = Test()
    >>> x()

    42


    Perhaps if you show us what you actually do, and what happens, we might
    be able to tell you what is happening. Please COPY AND PASTE the full
    traceback.



    --
    Steven
     
    Steven D'Aprano, Mar 4, 2012
    #2
    1. Advertising

  3. > Perhaps if you show us what you actually do, and what happens, we might
    > be able to tell you what is happening. Please COPY AND PASTE the full
    > traceback.


    Here is my code:
    # Trying to make callable staticmethod

    class sm(staticmethod):

    def __call__(self, *args, **kwargs):
    """ I know here is one more potential problem, because object
    passed instead of real class """
    return self.__get__(None, object)(*args, **kwargs)

    issubclass(sm, Callable)

    class Foo(object):

    @sm
    def bar():
    print("ololo")

    print("inside", bar, callable(bar), bar())

    if __name__=="__main__":
    print("class outise", Foo.bar, callable(Foo.bar), Foo.bar())
    f = Foo()
    print("instance outside", f.bar, callable(f.bar), f.bar())


    cpython output:
    ololo
    ('inside', <__main__.sm object at 0xb72b404c>, True, None)
    ololo
    ('class outise', <function bar at 0xb72a680c>, True, None)
    ololo
    ('instance outside', <function bar at 0xb72a680c>, True, None)

    jython output:
    Traceback (most recent call last):
    File "sm.py", line 17, in <module>
    class Foo(object):
    File "sm.py", line 23, in Foo
    print("inside", bar, callable(bar), bar())
    TypeError: 'staticmethod' object is not callable
     
    Sirotin Roman, Mar 4, 2012
    #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. David McNab
    Replies:
    0
    Views:
    520
    David McNab
    Apr 9, 2004
  2. Divya Prakash
    Replies:
    0
    Views:
    465
    Divya Prakash
    Dec 19, 2006
  3. Pinegar, Kent T

    FW: [Jython-users] Jython Licensing Question

    Pinegar, Kent T, Jul 24, 2008, in forum: Python
    Replies:
    0
    Views:
    372
    Pinegar, Kent T
    Jul 24, 2008
  4. exiquio
    Replies:
    2
    Views:
    564
    exiquio
    Oct 7, 2008
  5. Ulrich Eckhardt
    Replies:
    6
    Views:
    140
    Peter Otten
    Jul 12, 2013
Loading...

Share This Page