Python脚本出现类型错误';类型';对象是不可编辑的
我有一个email_check类,我用它作为脚本的一部分,最近它抛出了一些错误。我不得不对代码进行更改,因为它曾经使用过google plus,并因此引发了错误。我从下面代码中的for语句中删除了google plus,现在我得到类型错误“Type”对象不可编辑。代码如下:Python脚本出现类型错误';类型';对象是不可编辑的,python,python-3.x,Python,Python 3.x,我有一个email_check类,我用它作为脚本的一部分,最近它抛出了一些错误。我不得不对代码进行更改,因为它曾经使用过google plus,并因此引发了错误。我从下面代码中的for语句中删除了google plus,现在我得到类型错误“Type”对象不可编辑。代码如下: from scraper.config import Config # from scraper.google_plus import GooglePlus from scraper.scraper import Scrap
from scraper.config import Config
# from scraper.google_plus import GooglePlus
from scraper.scraper import Scraper
from scraper.spokeo import Spokeo
class EmailChecker:
def __init__(self):
config = Config()
# Open instance to chromedriver
self.__scraper = Scraper()
def check_email(self, email):
config = Config()
results = {}
# for _ in (GooglePlus, Spokeo):
for _ in (Spokeo):
site = _(self.__scraper)
try:
result = site.search_for_email(email)
except Exception:
if config.debug:
raise
result = None
try:
site.logout()
except Exception:
if config.debug:
raise
pass
results[_.__name__] = result
try:
self.__scraper.driver.close()
except Exception:
pass
try:
self.__scraper.driver.quit()
except Exception:
pass
return results
(GooglePlus,Spokeo)
是一个元组,可以在for
循环中迭代(Spokeo)
是括号内的表达式,仅用于表示优先级。对于一个更具体的例子,考虑<代码>(2+3, 1)< /> >(它评估为<代码>(5, 1)<代码> >与<代码>(2 +3)< /> >(计算为5)。
为了尽量少地修改代码,您可以编写(Spokeo,)
而不是(Spokeo)
来获得一个元组,尽管这有点奇怪。由于不再迭代任何内容,因此可以删除for循环:
results = {}
_ = Spokeo # the old for was here
site = _(self.__scraper)
...
<>但请考虑比<代码> > <代码>更好的名称。或者直接删除该变量并在其位置显式使用
Spokeo
:site=Spokeo(self.\uu scraper)
,依此类推。for(Spokeo,):
(带逗号),或者干脆删除for,然后使用=Spokeo
。不过,使用\uu
作为变量名可能不是最好的主意。请小心,这些代码中有很多都是不规范的!按照惯例,命名一个变量\uu
表示一个未使用/丢弃的值。你在做完全相反的事情<代码>例外情况除外:是不好的做法。您还可能错误地使用了双下划线(\uuuuu
)前缀。如果这些都是EmailChecker
的代码,我不相信类是构造这些代码的正确方法。