Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21185 > unrolled thread

Jython callable. How?

Started bySirotin Roman <web.elektro.net@gmail.com>
First post2012-03-04 16:03 +0700
Last post2012-03-04 21:14 +0700
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Jython callable. How? Sirotin Roman <web.elektro.net@gmail.com> - 2012-03-04 16:03 +0700
    Re: Jython callable. How? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-04 10:26 +0000
      Re: Jython callable. How? Sirotin Roman <web.elektro.net@gmail.com> - 2012-03-04 21:14 +0700

#21185 — Jython callable. How?

FromSirotin Roman <web.elektro.net@gmail.com>
Date2012-03-04 16:03 +0700
SubjectJython callable. How?
Message-ID<mailman.373.1330851839.3037.python-list@python.org>
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

[toc] | [next] | [standalone]


#21186

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-03-04 10:26 +0000
Message-ID<4f53433c$0$29989$c3e8da3$5496439d@news.astraweb.com>
In reply to#21185
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

[toc] | [prev] | [next] | [standalone]


#21199

FromSirotin Roman <web.elektro.net@gmail.com>
Date2012-03-04 21:14 +0700
Message-ID<mailman.381.1330870489.3037.python-list@python.org>
In reply to#21186
> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web