[Tutor] Function type?

Kalle Svensson kalle@lysator.liu.se
Tue Jun 10 15:09:02 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[Zak Arntson]
> Okay, so I can use isinstance as follows:
> 
> >>> isinstance('a', str)
> 1
> >>> isinstance(23, int)
> 1
> 
> But what's the shortcut keyword for a function type? Right now, I
> have to use a dummy function:
> 
> >>> def a(): pass
> >>> def squared(x): print x**2
> >>> isinstance(b, type(a))
> 1
> 
> Is there a keyword I'm being oblivious to? Or do I need to resort to
> this dummy function thing?

You can use types.FunctionType.  But what is it you are trying to do?
In my several years of programming Python, I haven't used many
isinstance calls that I couldn't replace with something better.
(There are a few, but that's the exception.)  Often, it is much more
useful to check for a property of the object being examined other than
type.  This means that objects of different types but with the
required property (e.g. being callable) can be used transparently.

Peace,
  Kalle
- -- 
Kalle Svensson, http://www.juckapan.org/~kalle/
Student, root and saint in the Church of Emacs.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>

iD8DBQE+5iySdNeA1787sd0RAt2NAKCefagAuZmEiWCAahULlm49qgy/jQCfRBpz
DNS1Df5E8FD/osS0XtTjOAg=
=9A2c
-----END PGP SIGNATURE-----