Built-in Functions Not Available

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2009
Posts: 3
Reputation: StMark is an unknown quantity at this point 
Solved Threads: 0
StMark StMark is offline Offline
Newbie Poster

Built-in Functions Not Available

 
0
  #1
Jul 30th, 2009
Hi

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

  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,054
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 265
Sponsor
jlm699's Avatar
jlm699 jlm699 is online now Online
Knows where his Towel is

Re: Built-in Functions Not Available

 
0
  #2
Jul 30th, 2009
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:
  1. def callable_(obj):
  2. if "__call()__" in dir(obj):
  3. return True
  4. else:
  5. return False
That should provide the same functionality
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 163
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 49
snippsat snippsat is online now Online
Junior Poster

Re: Built-in Functions Not Available

 
0
  #3
Jul 30th, 2009
Dont think you can call print,have you done that before?

In Python a callable is an object which type has a __call__ method:
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: StMark is an unknown quantity at this point 
Solved Threads: 0
StMark StMark is offline Offline
Newbie Poster

Re: Built-in Functions Not Available

 
0
  #4
Jul 31st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Built-in Functions Not Available

 
0
  #5
Jul 31st, 2009
Read the docs of Python3:
http://docs.python.org/3.1/whatsnew/...light=callable
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:
  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
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: StMark is an unknown quantity at this point 
Solved Threads: 0
StMark StMark is offline Offline
Newbie Poster

Re: Built-in Functions Not Available

 
0
  #6
Jul 31st, 2009
Well that explains it then!

Thanks. :-)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC