[Edu-sig] What is a Python module?
Corey Richardson
kb1pkl at aim.com
Tue May 3 12:48:26 CEST 2011
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 05/03/2011 03:39 AM, kirby urner wrote:
> I posed this question before. The usual answer is: a file containing
> Python source code.
>
> But you can import .pyc files even after the .py files are been
> removed, likewise .pyd files.
>
> Is a Python module anything that's importable and runnable within
> Python, that responds to dir( ) etc.?
>
> True whether or not said module comes with readable source code.
>
> In fact, one could argue a Python module is precisely *not* the
> readable source code .py file, as that *must* be compiled to byte
> codes first, and saved in the .pyc.
A module is an object like any other:
>>> import os
>>> type(os)
<type 'module'>
>>> a = os.__class__("hello!")
>>> type(a)
<type 'module'>
>>> dir(a)
['__doc__', '__name__']
You can even inherit from it!
>>> import os
>>> class MyModule(os.__class__):
... def __str__(self):
... return "hi!"
...
>>> a = MyModule("name")
>>> print a
hi!
Of course, if I ever see that in "real" code, I'd be worried.
- --
Corey Richardson
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJNv910AAoJEAFAbo/KNFvpohgH/jtY9D/uX+vLnwQHe3kz3fVf
egJ3SG/H3SsC/y8iRXpRiG0JvW8EOQ4iL6FqxhMZ0vl9uEj4u7cd1Q7ocrw08Yua
KVgjwfUJpFrLBKJzuDep6aThymmFmcpyYMdXC+sccHNklQQCi/kJa5KdK8+a7hvy
ZGEa+jBqALzFEBj4DyU1lrCcxUqJ2vPKprP/d+DRv+kgLMd/dTfeolkkOjiM966X
aexiV6Sj4MXg/51B9c2pJpZ1rngiRtJjXzagwRZFFpeDyThLCMkWgJcyIfFg4KgX
vphjPrn+jRdZKBI1lVHG1dpgyaskvrfxnyRaRtQtpuxVr2K7pgVJmp1+GB2ZmM8=
=3bbD
-----END PGP SIGNATURE-----
More information about the Edu-sig
mailing list