Ah, I love python. Oh no, it's Python. Here's a memo just for myself, it's quite conceptional, not just a copy from document. It will be updated at any time.
-
What does it mean by "built-in" modules and names?
It's with respect to the interpreter. Those modules built into the interpreter are built-in modules. When you start an interpreter, using the interactive environment in the command line or an IDE like Pycharm or whatever ways else, those modules are already there. If you check all names your current module already defines by using
dir(), you probably get a result like['__builtins__', '__doc__', '__name__', '__package__']
That means some actions are done when the interpreter starts. Here the value of the name
__builtins__is usually the standard module__builtin__. So if you further check all names defined in this module usingdir(__builtins__), you'll get a bunch of names like follows:['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']
Notice that this whole series of modules are already built in your interpreter, you can use or check them directly specifying their names without importing.
But what's a bit funny is that the module
__builtin__itself is not a built-in module. The value of the built-in variable__builtins__is this module. -
What is the list of paths used for the interpreter to search for modules?
The variable
sys.path. It's initialized with the environment variablePYTHONPATHor a built-in default. Its type is python list. -
How to check linear algebra library linked in numpy?
There's a question on stackoverflow, see the most voted answer, which is not the accepted one.
>>> import numpy as np >>> np.__config__.show()
-
What is metaclass?
There's a perfect answer on stackoverflow, see the most voted but not accepted one. But there's an even better article.
In short,
class(here we assume old-style class is gone from python, we will never use it) is a class,typeis also a class, and classes are all objects in python. When representing a noun, the wordclassis usually used for user-defined classes, andtypefor built-in classes.type(yourobject)is not a function but a class creator. However it often acts as a function. It creates an "type" object (a class), which is the type of this object.type(name, bases, dict)returns a new "type" object (a class), it's actually a dynamic form of theclassstatement. So in the second way,typecreates a class object, just asclassstatement does. What creates class objects is called metaclass.typeis the default metaclass used by python. You can set your metaclass to replacetypeto create a class, by putting__metaclass__=yourmetaclassunderclass Yourclass(baseclass):statement.Roughly speaking,
thisobject.__class__attribute is just "who creates this object". Ifthisobjectis a class, then the value ofthisobject.__class__is the metaclass who created it, usuallytype. -
What is proxy object?
Refer to this excellent article, and the document. In short, a proxy object can be used just like a strong reference of an object, say if
a = Foo(); b = weakref.proxy(a), then you can just useblikea,bis an alternative ofa. Whenais deleted, for example bydel a, then that object is deleted, and further using ofbwill raise exceptions.If
b = weakref.ref(a), thenbis a weak reference, which is a callable. We retrieve the pointed object by calling it:b(). Whenais deleted, then callingb()will getNonewithout any exception. -
What is closure?
Refer to this article. I'd like to just put the summary in that article here, because it's brief and clear enough:
- Closure is just a fancy name for a function that remembers the values from the enclosing lexical scope even when the program flow is no longer in the enclosing scope.
- If you've ever written a function that returned another function, you may have used closures.
So there's nothing deep or mysterious.
func.__closure__is a tuple including details of the variables defined in the enclosing scope, one can access the content of those variables usingfunc.__closure__[i].cell_contents. -
What is "method"?
Refer to this question, the answer by AndiDog, not the most voted one. In short, a method is a function defined in a class definition.
-
What is the use of
-min the commandpython -m themodule?Two uses. One is in the case that you're in some directory other than the path in
sys.path, but you want to run a built-in module, likeSimpleHTTPServer. Another is when your objective module is in the pathdira/, but it requires external modules indirb/, then you can walk intodirb/and runpython -m dira/yourmodule. The underlying reason is that-mwill search alongsys.path, and current working diretory will be the first path insys.path. By the way,__name__is always__main__with or without-m.
Comments
comments powered by Disqus