-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaticmethod.py
More file actions
55 lines (44 loc) · 1.42 KB
/
staticmethod.py
File metadata and controls
55 lines (44 loc) · 1.42 KB
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
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-04-03 21:05:08
# @copyright by hoojo@2018
# @changelog Added python3 `object -> @staticmethod` example
#
class RuntimeEnv:
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def __init__(self):
self.__BASE_PATH1 = os.path.dirname(os.path.abspath(__file__))
self.BASE_PATH2 = os.path.abspath(__file__)
# 需要 new 创建对象调用
def path(self):
print(f"项目根目录路径为:{self.BASE_PATH}")
print(f"当前执行脚本的目录路径为:{self.__BASE_PATH1}")
print(f"当前执行脚本的路径为:{self.BASE_PATH2}")
# get 属性,直接当做属性方法 RuntimeEnv.basepath
@property
def basepath(self):
return self.BASE_PATH
# set 属性,可以赋值
@basepath.setter
def basepath(self, path):
self.BASE_PATH = path
# 静态方法,直接调用,也能 new 实例访问
@staticmethod
def dir():
return "abcde"
# 直接访问,不能new 创建实例访问
def name():
print("name method")
print(RuntimeEnv.BASE_PATH)
RuntimeEnv.BASE_PATH = "abc"
RuntimeEnv().path()
print(RuntimeEnv().basepath)
env = RuntimeEnv()
env.basepath = "def"
print(env.basepath)
print(RuntimeEnv.dir())
print(RuntimeEnv().dir())
RuntimeEnv.name()