__init__() 缺少 1 个必需的位置参数:'city'
__init__() missing 1 required positional argument: 'city'
我从继承函数 News
中获取 PrivateAd
的父变量,但是当我 运行 具有所有参数的继承函数时,出现错误。
我如何运行PrivateAd
函数:
ad = PrivateAd(content=str(input('Input Ad content:')),
days=int(input('Input days:')),
city=str(input('Input Ad city:')))
ad.private_ad_print()
完整代码。
from datetime import datetime, date, timedelta
class News:
def __init__(self, content, city):
self.content = content
self.city = city
self.news_date_and_time = datetime.now().strftime("%d/%m/%Y %H:%M")
if not bool(self.content):
print("The content can't be null")
else:
self.content = content
if not bool(self.city):
print("The city can't be null")
else:
self.city = city
def news_print(self):
print(f"""News---------------
{self.content}
{self.city}, {self.news_date_and_time}""")
class PrivateAd(News):
def __init__(self, content, city, days):
News.__init__(self, content=content)
News.__init__(self, city=city)
self.days = days
self.ad_days_duration = timedelta(days=days) # специфический конструктор к классу рекламы
self.ad_start_date = datetime.now()
self.end_date = self.ad_start_date + self.ad_days_duration
def private_ad_print(self):
print(f"""Private Ad---------------
{self.content}
{self.city}, {self.end_date.strftime("%d/%m/%Y")}""")
我怎样才能 运行 PrivateAd
功能?
问题出在子 class、PrivateAd
构造函数上。在此 class 中,您应该在 parnet class 上调用 __init__
一次:
# ...
class PrivateAd(News):
def __init__(self, content, city, days):
News.__init__(self, content=content, city=city)
self.days = days
# rest of the code
# ...
通过编辑这部分,您的脚本 运行 不会有任何问题。
我从继承函数 News
中获取 PrivateAd
的父变量,但是当我 运行 具有所有参数的继承函数时,出现错误。
我如何运行PrivateAd
函数:
ad = PrivateAd(content=str(input('Input Ad content:')),
days=int(input('Input days:')),
city=str(input('Input Ad city:')))
ad.private_ad_print()
完整代码。
from datetime import datetime, date, timedelta
class News:
def __init__(self, content, city):
self.content = content
self.city = city
self.news_date_and_time = datetime.now().strftime("%d/%m/%Y %H:%M")
if not bool(self.content):
print("The content can't be null")
else:
self.content = content
if not bool(self.city):
print("The city can't be null")
else:
self.city = city
def news_print(self):
print(f"""News---------------
{self.content}
{self.city}, {self.news_date_and_time}""")
class PrivateAd(News):
def __init__(self, content, city, days):
News.__init__(self, content=content)
News.__init__(self, city=city)
self.days = days
self.ad_days_duration = timedelta(days=days) # специфический конструктор к классу рекламы
self.ad_start_date = datetime.now()
self.end_date = self.ad_start_date + self.ad_days_duration
def private_ad_print(self):
print(f"""Private Ad---------------
{self.content}
{self.city}, {self.end_date.strftime("%d/%m/%Y")}""")
我怎样才能 运行 PrivateAd
功能?
问题出在子 class、PrivateAd
构造函数上。在此 class 中,您应该在 parnet class 上调用 __init__
一次:
# ...
class PrivateAd(News):
def __init__(self, content, city, days):
News.__init__(self, content=content, city=city)
self.days = days
# rest of the code
# ...
通过编辑这部分,您的脚本 运行 不会有任何问题。