This product is not supported for your selected Datadog site. ().
Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
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