You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+26-5Lines changed: 26 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,23 +37,45 @@ You could define an alias for the base class, assign the real base class to it b
37
37
38
38
#### How do I find the current module name?
39
39
40
-
A module can find out its own module name by looking at the predefined global variable __name__. If this has the value '__main__', the program is running as a script. Many modules that are usually used by importing them also provide a command-line interface or a self-test, and only execute this code after checking __name__: def main(): print 'Running test...' ... if __name__ == '__main__': main() __import__('x.y.z') returns Try: __import__('x.y.z').y.z For more realistic situations, you may have to do something like m = __import__(s) for i in s.split(".")[1:]: m = getattr(m, i)
40
+
A module can find out its own module name by looking at the predefined global variable __name__.
41
+
If this has the value '__main__', the program is running as a script.
42
+
Many modules that are usually used by importing them also provide a command-line interface or a self-test, and only execute this code after checking __name__:
43
+
```python
44
+
defmain():
45
+
print'Running test...'
46
+
if__name__=='__main__':
47
+
main()
48
+
49
+
__import__('x.y.z')
50
+
returns Try: __import__('x.y.z').y.z
51
+
52
+
# For more realistic situations, you may have to do something like:
53
+
m =__import__(s)
54
+
for i in s.split(".")[1:]: m =getattr(m, i)
55
+
```
41
56
42
57
#### How do I copy a file?
43
58
44
59
The shutil module contains a copyfile() function.
45
60
46
61
#### How do I access a module written in Python from C?
47
62
48
-
You can get a pointer to the module object as follows: module = PyImport_ImportModule(""); If the module hasn't been imported yet (i.e. it is not yet present in sys.modules), this initializes the module; otherwise it simply returns the value of sys.modules[""]. Note that it doesn't enter the module into any namespace -- it only ensures it has been initialized and is stored in sys.modules. You can then access the module's attributes (i.e. any name defined in the module) as follows: attr = PyObject_GetAttrString(module, ""); Calling PyObject_SetAttrString() to assign to variables in the module also works.
63
+
You can get a pointer to the module object as follows: module = PyImport_ImportModule(""); If the module hasn't been imported yet (i.e. it is not yet present in sys.modules), this initializes the module; otherwise it simply returns the value of ``sys.modules[""]``. Note that it doesn't enter the module into any namespace -- it only ensures it has been initialized
64
+
and is stored in sys.modules. You can then access the module's attributes (i.e. any name defined in the module) as follows: attr = PyObject_GetAttrString(module, ""); Calling PyObject_SetAttrString() to assign to variables in the module also works.
49
65
50
66
#### How do I interface to C++ objects from Python?
51
67
52
68
Depending on your requirements, there are many approaches. To do this manually, begin by reading the "Extending and Embedding" document. Realize that for the Python run-time system, there isn't a whole lot of difference between C and C++ -- so the strategy of building a new Python type around a C structure (pointer) type will also work for C++ objects.
53
69
54
70
#### How can I pass optional or keyword parameters from one function to another?
55
71
56
-
Collect the arguments using the * and ** specifier in the function's parameter list; this gives you the positional arguments as a tuple and the keyword arguments as a dictionary. You can then pass these arguments when calling another function by using * and **: def f(x, *tup, **kwargs): ... kwargs['width']='14.3c' ... g(x, *tup, **kwargs) In the unlikely case that you care about Python versions older than 2.0, use 'apply': def f(x, *tup, **kwargs): ... kwargs['width']='14.3c' ... apply(g, (x,)+tup, kwargs)
72
+
Collect the arguments using the * and ** specifier in the function's parameter list; this gives you the positional arguments as a tuple and the keyword arguments as a dictionary. You can then pass these arguments when calling another function by using ``*`` and ``**``:
73
+
```
74
+
def f(x, *tup, **kwargs):
75
+
kwargs['width']='14.3c'
76
+
g(x, *tup, **kwargs)
77
+
```
78
+
In the unlikely case that you care about Python versions older than 2.0, use 'apply': def f(x, *tup, **kwargs): ... kwargs['width']='14.3c' ... apply(g, (x,)+tup, kwargs)
57
79
58
80
#### How do you make a higher order function in Python?
59
81
@@ -65,8 +87,7 @@ To convert, e.g., the number 144 to the string '144', use the built-in function
65
87
66
88
#### What are the disadvantages of the Python programming language?
67
89
68
-
It is not best for mobile application
69
-
Due to interpreter its execution speed is not up to mark as compared to compiler
90
+
It is not best for mobile application. Due to interpreter its execution speed is not up to mark as compared to compiler.
70
91
71
92
#### How is the Implementation of Python's dictionaries done?
0 commit comments