other ways to check for <type 'function'>?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christophe

    #16
    Re: other ways to check for &lt;type 'function'&gt;?

    Christophe a écrit :
    Sybren Stuvel a écrit :
    >Christophe enlightened us with:
    >>I don't think it's a good idea because when you place a try catch
    >>block around a function call, you'll catch any exception thrown by
    >>the function itself and not only the "cannot be called" exception.
    >>
    >That depends on the exception you're catching, doesn't it?
    >
    And what if the function has an error and calls a non callable object ?
    Or even worse. Since we have :
    >>None()
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: 'NoneType' object is not callable
    >>1+""
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: unsupported operand type(s) for +: 'int' and 'str'
    >>""+1
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: cannot concatenate 'str' and 'int' objects

    All of them are TypeError exceptions. It is extremly dangerous to put a
    try except TypeError clause around a function call to catch the case
    where the function object isn't a callable :/

    Comment

    • Fredrik Lundh

      #17
      Re: other ways to check for &lt;type 'function'&gt;?

      elderic wrote:
      I just wanted to know if there was a keyword for functions, too.
      >
      Then u could've done: type(f) is function
      quite similar to: type(x) is int
      but why do you think you need that, when you have callable() ? unless
      you're doing specialized stuff, there's really no reason to distinguish
      between function objects and other callables.

      </F>

      Comment

      • elderic

        #18
        Re: other ways to check for &lt;type 'function'&gt;?

        I just wanted to know if there was a keyword for functions, too.

        Then u could've done: type(f) is function
        quite similar to: type(x) is int
        >
        but why do you think you need that, when you have callable() ? unless
        you're doing specialized stuff, there's really no reason to distinguish
        between function objects and other callables.
        >
        </F>
        I never said that I need anything - I merely asked for alternatives. =)
        I'm pretty happy with the types-module or the callable() test.

        Basically it's just for wrapping the creation of Tk-Buttons, etc.
        They need a callback and in my process to learn I wanted to know about
        the
        possible options to test for that.

        peace of mind. =)
        elderic

        Comment

        • Fredrik Lundh

          #19
          Re: other ways to check for &lt;type 'function'&gt;?

          elderic wrote:
          I never said that I need anything - I merely asked for alternatives. =)
          I'm pretty happy with the types-module or the callable() test.
          >
          Basically it's just for wrapping the creation of Tk-Buttons, etc.
          They need a callback and in my process to learn I wanted to know about
          the possible options to test for that.
          the more reason not to even think about using anything but "callable".
          many things in Python are callable; an explicit function test would
          disallow several interesting alternatives:
          >>def foo():
          .... pass
          ....
          >>class fum:
          .... def bar(self):
          .... pass
          .... def __call__(self):
          .... pass
          ....
          >>type(foo) is type(fum)
          False
          >>f = fum()
          >>type(foo) is type(f)
          False
          >>type(foo) is type(f.bar)
          False

          etc.

          (and I really shouldn't ask why you cannot just use Tkinter, and spend
          your time and energy on something more worthwhile ? if not else, there
          are plenty of Tk extensions that could need nice wrappers...)

          </F>

          Comment

          • eduardo.padoan@gmail.com

            #20
            Re: other ways to check for &lt;type 'function'&gt;?

            Python should have a TypeError subclass: "NotCallableErr or", to enforce
            BAFP.

            Comment

            • Ben Finney

              #21
              Re: other ways to check for &lt;type 'function'&gt;?

              "eduardo.padoan @gmail.com" <eduardo.padoan @gmail.comwrite s:
              Python should have a TypeError subclass: "NotCallableErr or", to
              enforce BAFP.
              Bureau of Alcohol, Firearms, and Programmers?

              (Perhaps you meant "EAFP")

              --
              \ "If you go flying back through time and you see somebody else |
              `\ flying forward into the future, it's probably best to avoid eye |
              _o__) contact." -- Jack Handey |
              Ben Finney

              Comment

              Working...