我很难理解使用实例方法与静态方法的区别。另外,我不知道我的函数是否是静态的,因为没有@staticmethod decorator。当我调用其中一个方法时,我能访问类函数吗?在
我正在研究一个能把信息发送到数据库的webscraper。它的设置是每周运行一次。我的代码结构如下所示import libraries...
class Get:
def build_url(url_paramater1, url_parameter2, request_date):
return url_with_parameters
def web_data(request_date, url_parameter1, url_parameter2): #no use of self
# using parameters pull the variables to look up in the database
for a in db_info:
url = build_url(a, url_parameter2, request_date)
x = requests.Session().get(url, proxies).json()
#save data to the database
return None
#same type of function for pulling the web data from the database and parsing it
if __name__ == ‘__main__’:
Get.web_data(request_date, url_parameter1, url_parameter2)
Parse.web_data(get_date, parameter) #to illustrate the second part of the scrapper
这就是基本结构。代码是功能性的,但我不知道我是否在使用这些方法(函数?)正确而且可能会错过将来使用我的代码的方法。我甚至可能在编写错误的代码,这些代码会导致错误,这些错误很难调试,只是因为我没有遵循最佳实践。在
在阅读了有关何时使用类和实例方法之后。我不明白我为什么要用它们。如果我想建立url或从网站上提取数据,我调用build_url或get_web_data函数。我不需要函数的实例来单独跟踪任何东西。我无法想象什么时候我需要把一些东西分开,我认为这也是问题的一部分。在
我认为我的问题与前面的问题不同的原因是:当我坐下来写代码时,用来解释差异的概念性示例似乎对我没有帮助。我还没有遇到过用不同的方法解决的实际问题,这些方法显示了我何时应该使用实例方法,但是在查看代码的概念性示例时,实例方法似乎是必需的。在
谢谢你!在