Skip to content

Commit 652b31b

Browse files
committed
add 单例import方法
1 parent a8af392 commit 652b31b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Readme.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,25 @@ class MyClass:
453453
...
454454
```
455455

456+
### 4 import方法
457+
458+
作为python的模块是天然的单例模式
459+
460+
```python
461+
# mysingleton.py
462+
class My_Singleton(object):
463+
def foo(self):
464+
pass
465+
466+
my_singleton = My_Singleton()
467+
468+
# to use
469+
from mysingleton import my_singleton
470+
471+
my_singleton.foo()
472+
473+
```
474+
456475
## 17 Python中的作用域
457476

458477
Python 中,一个变量的作用域总是由在代码中被赋值的地方所决定的。

0 commit comments

Comments
 (0)