Skip to content

Commit 0e5a732

Browse files
authored
Update README.md
1 parent e0933e9 commit 0e5a732

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,45 @@ You could define an alias for the base class, assign the real base class to it b
3737

3838
#### How do I find the current module name?
3939

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+
def main():
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+
```
4156

4257
#### How do I copy a file?
4358

4459
The shutil module contains a copyfile() function.
4560

4661
#### How do I access a module written in Python from C?
4762

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.
4965

5066
#### How do I interface to C++ objects from Python?
5167

5268
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.
5369

5470
#### How can I pass optional or keyword parameters from one function to another?
5571

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)
5779

5880
#### How do you make a higher order function in Python?
5981

@@ -65,8 +87,7 @@ To convert, e.g., the number 144 to the string '144', use the built-in function
6587

6688
#### What are the disadvantages of the Python programming language?
6789

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.
7091

7192
#### How is the Implementation of Python's dictionaries done?
7293

0 commit comments

Comments
 (0)