This product is not supported for your selected Datadog site. ().
이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다. 현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.
Metadata
ID:python-best-practices/init-method-required
Language: Python
Severity: Notice
Category: Best Practices
Description
Ensure that a class has an __init__ method. This check is bypassed when the class is a data class (annotated with @dataclass).
Non-Compliant Code Examples
classFoo:# need to define __init__deffoo(bar):passdefbar(baz):pass
Compliant Code Examples
# dataclass do not require an init class@dataclassclassRequests:cpu:float# expressed in cpu coresmemory:int# expressed in bytes@staticmethoddeffrom_pod(pod:V1Pod):cpu=0.0memory=0forcontainerinpod.spec.containers:cpu+=parse_cpu_string(container.resources.requests["cpu"])memory+=parse_memory_string(container.resources.requests["memory"])returnRequests(cpu,memory)defadd(self,other):self.cpu+=other.cpuself.memory+=other.memory@dataclasses.dataclassclassDependencyKey:name:strversion:strdef__hash__(self):ifself.versionisNone:version=""else:version=self.versionreturnhash(self.name+version)@frozenclassAnotherClass:cpu:floatmemory:intdefadd(self,other):self.cpu+=other.cpuself.memory+=other.memory@dataclasses.dataclass(frozen=True)classSettings:keys:list[[str,str]]defget_keys(self):returnself.keys