- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Python 非常陌生(大约 3 周前开始),我正在尝试制作一个脚本来抓取网页以获取信息。检索到信息后,它会通过一个函数对其进行格式化,然后将其传递给一个采用 17 个变量作为参数的类。该类使用此信息来计算其他一些变量,并且当前有一个构造字典的方法。代码按预期工作,但我在 Pycharm 中使用的一个名为 SonarLint 的插件突出显示 17 个变量太多而无法用作参数?
我一直在寻找将信息传递给类的替代方法,例如在元组或列表中,但找不到太多似乎相关的信息。将许多变量作为参数传递给类的最佳实践是什么?或者我根本不应该使用类来做这种事情?
为了便于阅读,我减少了变量和代码的数量,但这是类;
Class GenericEvent:
def __init__(self, type, date_scraped, date_of_event, time, link,
blurb):
countdown_delta = date_of_event - date_scraped
countdown = countdown_delta.days
if countdown < 0:
has_passed = True
else:
has_passed = False
self.type = type
self.date_scraped = date_scraped
self.date_of_event = date_of_event
self.time = time
self.link = link
self.countdown = countdown
self.has_passed = has_passed
self.blurb = blurb
def get_dictionary(self):
event_dict = {}
event_dict['type'] = self.type
event_dict['scraped'] = self.date_scraped
event_dict['date'] = self.date_of_event
event_dict['time'] = self.time
event_dict['url'] = self.link
event_dict['countdown'] = self.countdown
event_dict['blurb'] = self.blurb
event_dict['has_passed'] = self.has_passed
return event_dict
在通过以下方式清理数据后,我一直将变量作为键:值对传递给类:
event_info = GenericEvent(type="Lunar"
date_scraped=30/01/19
date_of_event=28/07/19
time=12:00
link="www.someurl.com"
blurb="Some string.")
并通过调用检索字典:
event_info.get_dictionary()
我打算向该类添加其他方法,以便也能够执行其他操作(不仅仅是创建 1 个字典),但希望在扩展该类的功能之前解决这个问题。
任何帮助或链接将不胜感激!
最佳答案
一个选项是命名元组:
from typing import Any, NamedTuple
class GenericEvent(NamedTuple):
type: Any
date_scraped: Any
date_of_event: Any
time: Any
link: str
countdown: Any
blurb: str
@property
def countdown(self):
countdown_delta = date_of_event - date_scraped
return countdown_delta.days
@property
def has_passed(self):
return self.countdown < 0
def get_dictionary(self):
return {
**self._asdict(),
'countdown': self.countdown,
'has_passed': self.has_passed,
}
(将Any
替换为字段的实际类型,例如datetime.datetime
。)
或者,如果您希望它是可变的,则 data class .
关于python - 方法 __init__ 参数过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55447102/
为什么正是是 A.__init__() B.__init__() D.__init__() 由以下代码打印?特别是: 为什么是C.__init__() 未打印? 为什么是C.__init__()如果我
目前我有这样的事情: @dataclass(frozen=True) class MyClass: a: str b: str c: str d: Dict[str, str] ...
我正在尝试从父类继承属性: class Human: def __init__(self,name,date_of_birth,gender,nationality): self.name =
如何扩展基类的 __init__,添加更多要解析的参数,而不需要 super().__init__(foo, bar) 在每个派生类中? class Ipsum: """ A base ips
这是我试图解决的一个非常简单的例子: class Test(object): some_dict = {Test: True} 问题是我无法在 Test 仍在定义时引用它 通常,我会这样做:
我在 Objective-C 中使用过这个结构: - (void)init { if (self = [super init]) { // init class }
我有一个类层次结构,其中 class Base 中的 __init__ 执行一些预初始化,然后调用方法 calculate。 calculate 方法在 class Base 中定义,但预计会在派生类
这是我在多种语言中都怀念的一个特性,想知道是否有人知道如何在 Python 中完成它。 我的想法是我有一个基类: class Base(object): def __init__(self):
我正在对 threading.Thread 类进行子类化,它目前看起来像这样: class MyThread(threading.Thread): def __init__(self:
我正在用 cython 写一些代码,我有一些 "Packages “within” modules" . — 这实际上是对我在那里的问题的跟进,结构应该是一样的。问题是这是 cython,所以我处理的
class AppendiveDict(c.OrderedDict): def __init__(self,func,*args): c.OrderedDict.__init_
看完this回答,我明白 __init__ 之外的变量由类的所有实例和 __init__ 内的变量共享每个实例都是唯一的。 我想使用所有实例共享的变量,随机给我的类实例一个唯一的参数。这是我尝试过的较
在下面的代码中: import tkinter as tk class CardShuffling(tk.Tk): background_colour = '#D3D3D3'
我正在覆盖类的 __new__() 方法以返回具有特定 __init__() 集的类实例。 Python 似乎调用类提供的 __init__() 方法而不是特定于实例的方法,尽管 Python 文档在
从内置类型和其他类派生时,内置类型的构造函数似乎没有调用父类(super class)构造函数。这会导致 __init__ 方法不会被 MRO 中内置函数之后的类型调用。 例子: class A:
答: super( BasicElement, self ).__init__() 乙: super( BasicElement, self ).__init__( self ) A 和 B 有什么区
class A(object): def __init__(self): print('A.__init__()') class D(A): def __init__(
到目前为止我已经成功地做了什么: 我创建了一个 elem 类来表示 html 元素(div、html、span、body 等)。 我可以像这样派生这个类来为每个元素创建子类: class elem:
我一直在努力理解 super() 在多重继承的上下文中的行为。我很困惑为什么在 test2.py 的父类中调用 super() 会导致为父类调用 __init__()? test1.py #!/usr
为什么我在 Python 代码中看不到以下内容? class A: def __init__(self, ...): # something important class B
我是一名优秀的程序员,十分优秀!