[英]Python Methods: Returning data vs Storing data in attributes
This might be a general programming question rather than Python specific, but, what is the best practice when it comes validation classes? 这可能是一个通用的编程问题,而不是特定于Python的,但是,当涉及到验证类时,最佳实践是什么?
My first approach is having the method return a dictionary: 我的第一种方法是让该方法返回字典:
class Validator():
def scene(self):
# Do validations here
return {'result': True, 'data' my_data, 'external_references': my_references}
def character(self):
# Do validations here
return {'result': True, 'data' my_data, 'external_references': my_references}
# Usage
v = Validator()
scene_results = v.scene()
character_results = v.character()
if scene_results['result']:
print "Scene Validation Succesful!"
print "Data:", scene_results['data'], ", Files:", scene_results['external_references']
if character_results['result']:
print "Character Validation Succesful!"
print "Data:", character_results['data'], ", Files:", character_results['external_references']
My second approach is to populate the class attributes instead: 我的第二种方法是填充类属性:
class Validator():
def __init__(self):
self.scene_result = None
self.scene_data = None
self.scene_external_references = None
self.character_result = None
self.character_data = None
self.character_external_references = None
def scene(self):
## Do validations here
self.scene_result = True
self.scene_data = my_data
self.scene_external_references = my_references
# Won't return the values
def character(self):
# Do validations here
self.scene_result = True
self.scene_data = my_data
self.scene_external_references = my_references
# Won't return the values
# Usage
v = Validator()
v.scene()
v.character()
if v.scene_result:
print "Scene Validation Succesful!"
print "Data:", v.scene_data, ", Files:", v.scene_external_references
if v.character_result:
print "Character Validation Succesful!"
print "Data:", v.character_data, ", Files:", v.character_external_references
Or should I use both approaches to get the best of both worlds? 还是我应该同时使用两种方法来兼顾两者?
Both of the examples work fine, however I want to know if this is the conventional way a validation class should work. 两个示例都可以正常工作,但是我想知道这是否是验证类的常规工作方式。 Sorry if the answer is too obvious but I am learning here.
抱歉,如果答案太明显,但我在这里学习。 Thanks in advance.
提前致谢。
Well, if there are larger chunks of data to return and reuse, then i recommend the second approach since in that case you merely reference the data somewhere stored, whilst in the first case you also have to copy the returned value into a new variable, which could, for larger values, slow down the program. 好吧,如果有更大的数据块要返回和重用,那么我建议使用第二种方法,因为在这种情况下,您仅引用存储的数据,而在第一种情况下,您还必须将返回的值复制到新变量中,对于较大的值,可能会使程序变慢。 The second approach also provides encasement which is always good for security and also readabilty, so my recommendation sounds:
第二种方法还提供了对安全性和可读性总是有益的包装,因此我的建议听起来是:
use classes where possible. 尽可能使用类。
I also recommend writing separate test methods for the "validations" you talk about, for the same reasons as the previous. 我也建议出于上述原因,为您讨论的“验证”编写单独的测试方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.