-
Notifications
You must be signed in to change notification settings - Fork 13
/
dunders.Rmd
83 lines (62 loc) · 1.99 KB
/
dunders.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
---
jupyter:
orphan: true
jupytext:
notebook_metadata_filter: all,-language_info
split_at_heading: true
text_representation:
extension: .Rmd
format_name: rmarkdown
format_version: '1.2'
jupytext_version: 1.13.7
kernelspec:
display_name: Python 3
language: python
name: python3
---
# Two double underscore variables
Python often uses variable and function and method names with double
underscores on each end.
For example, as Python sets up to import a module, it defines a variable for
itself called `__file__`.
Experienced Python people often call these variables "dunder" variables,
because they have Double UNDERscores on each side.
When you see a *dunder* variable or function or method, it is almost invariably
a variable or function or method that Python has defined, or that Python is
using in a special way.
## The `__file__` variable
The `__file__` variable contains the path to the file that Python is currently
importing. You can use this variable inside a module to find the path of the
module. For example, let's say you have a module like this:
```{python}
# %%file example_module.py
# An example Python module
print("Type of __file__ variable is:", type(__file__))
print("__file__ is:", __file__)
```
If you run this module as a script, `__file__` is set:
```{python}
# Execute as script
%run example_module.py
```
If you `import` the module, `__file__` is also set:
```{python}
import example_module
```
## The `__name__` variable
When Python `import`s a module, it sets the `__name__` variable to be a string
containing the name of the module it is importing:
```{python}
# %%file another_example.py
# Another example Python module
print("Type of __name__ variable is:", type(__name__))
print("__name__ is:", __name__)
```
```{python}
import another_example
```
If you run the same module as a script, Python is not `import`ing when it runs
the code, and `__name__` contains the string `"__main__"`:
```{python}
%run another_example.py
```