Skip to content

Commit ef9c484

Browse files
author
tuntun
committed
Python hasattr判断是否存在属性
1 parent 4782217 commit ef9c484

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

hasattr.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Python hasattr判断是否存在属性
4+
5+
class Test:
6+
7+
product11 = ['d']
8+
9+
def __init__(self):
10+
pass
11+
12+
def product(self, a, b):
13+
return a * b
14+
15+
def is_exists(self):
16+
print(hasattr(self, 'product'))
17+
18+
19+
test = Test()
20+
test.is_exists()
21+
22+
23+
"""
24+
dir(test):
25+
['__doc__', '__init__', '__module__', 'is_exists', 'product', 'product11']
26+
"""

0 commit comments

Comments
 (0)