This page is not yet available in Spanish. We are working on its translation. If you have any questions or feedback about our current translation project, feel free to reach out to us!
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