943,900 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 674
  • Python RSS
Jul 30th, 2009
0

Built-in Functions Not Available

Expand Post »
Hi

My "callable" function doesn't work. Who knows why?

Python Syntax (Toggle Plain Text)
  1. >>> callable(print)
  2. Traceback (most recent call last):
  3. File "<pyshell#0>", line 1, in <module>
  4. callable(print)
  5. NameError: name 'callable' is not defined
  6. >>>

Regards,

Mark
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
StMark is offline Offline
3 posts
since May 2009
Jul 30th, 2009
0

Re: Built-in Functions Not Available

What version of python are you using? Did it work before and stop or just never work at all? Are any other built-in functions working?

As a replacement you could use this:
python Syntax (Toggle Plain Text)
  1. def callable_(obj):
  2. if "__call()__" in dir(obj):
  3. return True
  4. else:
  5. return False
That should provide the same functionality
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Jul 30th, 2009
0

Re: Built-in Functions Not Available

Dont think you can call print,have you done that before?

In Python a callable is an object which type has a __call__ method:
python Syntax (Toggle Plain Text)
  1. def test():
  2. print 'hi'
  3.  
  4. >>> dir(test)
  5. ['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
  6.  
  7. >>> callable(test)
  8. True
  9.  
  10. >>> a = 5
  11. >>> dir(a)
  12. ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
  13.  
  14. >>> callable(a)
  15. False
  16. >>>
Last edited by snippsat; Jul 30th, 2009 at 10:13 am.
Reputation Points: 280
Solved Threads: 278
Master Poster
snippsat is offline Offline
771 posts
since Aug 2008
Jul 31st, 2009
0

Re: Built-in Functions Not Available

I'm using version 3.1 which I recently installed. I'm just starting to learn Python so this is the first time I have tried to use the function and it doesn't work. Thanks for the code suggestion, but I'm more concerned about why it doesn't work when it is supposed to be a built-in function. I'm trying to learn from a book, but I can't get the examples to work. Some built-in functions (e.g. abs()) do work.

Snippsat, as far as I understand, callable(print) should return "False" because print is not callable.

Does anyone know what module callable is defined in and how this module is loaded? Maybe abs() is in a different module. Maybe my installation is missing something in the set-up.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
StMark is offline Offline
3 posts
since May 2009
Jul 31st, 2009
0

Re: Built-in Functions Not Available

Read the docs of Python3:
http://docs.python.org/3.1/whatsnew/...light=callable
Quote ...
Removed callable(). Instead of callable(f) you can use hasattr(f, '__call__'). The operator.isCallable() function is also gone.
You can run this program to check what you have available:
python Syntax (Toggle Plain Text)
  1. # print all builtin functions/methods
  2.  
  3. # do a case insensitive sort then print ...
  4. print( '\n'.join(sorted(dir(__builtins__), key=str.lower)) )
  5.  
  6. # or send to a file ...
  7. fout = open('Builtins.txt', 'w')
  8. print( '\n'.join(sorted(dir(__builtins__), key=str.lower)), file=fout)
  9.  
  10. """my result with Python 3.1 -->
  11. __build_class__
  12. __debug__
  13. __doc__
  14. __import__
  15. __name__
  16. __package__
  17. abs
  18. all
  19. any
  20. ArithmeticError
  21. ascii
  22. AssertionError
  23. AttributeError
  24. BaseException
  25. bin
  26. bool
  27. BufferError
  28. bytearray
  29. bytes
  30. BytesWarning
  31. chr
  32. classmethod
  33. compile
  34. complex
  35. copyright
  36. credits
  37. delattr
  38. DeprecationWarning
  39. dict
  40. dir
  41. divmod
  42. Ellipsis
  43. enumerate
  44. EnvironmentError
  45. EOFError
  46. eval
  47. Exception
  48. exec
  49. exit
  50. False
  51. filter
  52. float
  53. FloatingPointError
  54. format
  55. frozenset
  56. FutureWarning
  57. GeneratorExit
  58. getattr
  59. globals
  60. hasattr
  61. hash
  62. help
  63. hex
  64. id
  65. ImportError
  66. ImportWarning
  67. IndentationError
  68. IndexError
  69. input
  70. int
  71. IOError
  72. isinstance
  73. issubclass
  74. iter
  75. KeyboardInterrupt
  76. KeyError
  77. len
  78. license
  79. list
  80. locals
  81. LookupError
  82. map
  83. max
  84. MemoryError
  85. memoryview
  86. min
  87. NameError
  88. next
  89. None
  90. NotImplemented
  91. NotImplementedError
  92. object
  93. oct
  94. open
  95. ord
  96. OSError
  97. OverflowError
  98. PendingDeprecationWarning
  99. pow
  100. print
  101. property
  102. quit
  103. range
  104. ReferenceError
  105. repr
  106. reversed
  107. round
  108. RuntimeError
  109. RuntimeWarning
  110. set
  111. setattr
  112. slice
  113. sorted
  114. staticmethod
  115. StopIteration
  116. str
  117. sum
  118. super
  119. SyntaxError
  120. SyntaxWarning
  121. SystemError
  122. SystemExit
  123. TabError
  124. True
  125. tuple
  126. type
  127. TypeError
  128. UnboundLocalError
  129. UnicodeDecodeError
  130. UnicodeEncodeError
  131. UnicodeError
  132. UnicodeTranslateError
  133. UnicodeWarning
  134. UserWarning
  135. ValueError
  136. vars
  137. Warning
  138. ZeroDivisionError
  139. zip
  140. """
Last edited by sneekula; Jul 31st, 2009 at 12:22 pm. Reason: code
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Jul 31st, 2009
0

Re: Built-in Functions Not Available

Well that explains it then!

Thanks. :-)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
StMark is offline Offline
3 posts
since May 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Can I make an Invisible Cursor? (Tkinter)
Next Thread in Python Forum Timeline: Is this even possible ? Email Issue





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC