举个例子
class A:
def __init__(self,value1,value2):
self.value1=value1
self.value2=value2
def function(value):
#do some thing and get the name of obj.property
print(property)
tempObj=A()
function(tempObj.value1)#这里会print输出value1,无论value1值是多少
function(tempObj.value2)#这里会print输出value2,无论value2值是多少
真实遇到的问题是这样的
在写restfulApi,每次会在逻辑代码前要检查传入数据的正确性,经常会检查数组的长度
然后大概写成这样
def post(self):
parsed_data = self.parser.parse_args()
if len(parsed_data.T) < 8:
return make_parser_error('T', '不得少于8个')
我想把代码变成
def post(self):
parsed_data = self.parser.parse_args()
check_result=function(parsed_data.T,8)
if check_result:
return check_result,400 #400是返回状态码
It should not be possible to obtain variable names through variables in Python. At most, the class name can be obtained, because Python is a value type. If value1 and value2 have the same value, then they are exactly the same objects and point to the same place in the memory
python3
__dict__