__init__ method is a generator¶
ID: py/init-method-is-generatorKind: problemSecurity severity: Severity: errorPrecision: very-highTags: - quality - reliability - correctnessQuery suites: - python-security-and-quality.qls
Click to see the query in the CodeQL repository
The__init__ method of a class is used to initialize new objects, not create them. As such, it should not return any value. By including ayield expression in the method turns it into a generator method. On calling it will return a generator resulting in a runtime error.
Recommendation¶
The presence of ayield expression in an__init__ method suggests a logical error, so it is not possible to suggest a general fix.
Example¶
In this example the__init__ method contains a yield expression. This is not logical in the context of an initializer.
classInitIsGenerator(object):def__init__(self,i):yieldi
References¶
Python:Theinit method.