我的代码中出现了一个奇怪的错误。
以下是摘录。
from sqlalchemy import *
import datetime
from config import *
from sqlalchemy.orm import sessionmaker
import logging
engine = create_engine(DB_CONNECTION_STRING)
metadata = MetaData(engine)
Session = sessionmaker(bind=engine)
def now():
return datetime.datetime.now()
Feeds = Table('feeds', metadata,
Column('id', Integer, primary_key=True),
Column('title', String(500)),
Column('summary', Text),
Column('link', String(200)),
Column('published_at', DateTime),
Column('created_at', DateTime),
Column('updated_at', DateTime),
Column('user_seen', DateTime),
Column('rss_link_id', ForeignKey('rss_links.id', ondelete="CASCADE"))
)
RssLinks = Table('rss_links', metadata,
Column('id', Integer, primary_key=True),
Column('link', String(200)),
Column('rss_link', String(200)),
Column('title', String(500)),
Column('status', String(100)),
Column('created_at', DateTime),
Column('updated_at', DateTime),
)
def insert_into_rss_links(link=link, rss_link=rss_link, title=title):
ins = RssLinks.insert()
ins.execute(link=link, rss_link=rss_link, title=title, status='VALID', created_at=now())
def insert_rss_links(site_link, _rss_links):
for rss_link in _rss_links:
insert_into_rss_links(link=site_link, rss_link=_rss_links['link'], title=_rss_links['title'])
if __name__ == '__main__':
metadata.create_all()
错误是
In [1]: import dbi
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-81964b779871> in <module>()
----> 1 import dbi
/home/asit/own/proj/dbi.py in <module>()
44
---> 45 def insert_into_rss_links(link=link, rss_link=rss_link, title=title):
46 ins = RssLinks.insert()
47 ins.execute(link=link, rss_link=rss_link, title=title, status='VALID', created_at=now())
NameError: name 'link' is not defined
有人可以告诉我,为什么会这样?
答案 0 :(得分:2)
您正在定义函数,就像您调用它一样:
def insert_into_rss_links(link=link, rss_link=rss_link, title=title):
在创建函数对象时,link
参数的默认值没有link
。
答案 1 :(得分:2)
您提供的insert_into_rss_links()
参数默认值为:
def insert_into_rss_links(link=link, rss_link=rss_link, title=title):
定义函数时,每个keyword=value
部分必须具有有效的value
表达式,但link
,rss_link
和title
不是。
最有可能的是,你并不意味着在中提供这些默认值;只需删除它们:
def insert_into_rss_links(link, rss_link, title):