Example code:
class MiNiCarStore(CarStore):
def createCar(self, typeName):
self.carFactory = CarFactory() # An underline prompt will appear: This inspection detects instance attribute definition outside __init__ method
return self.carFactory.createCar(typeName)
The reason is: according to the principle of SRP (single scalability principle, SRP), this class should assume a certain interface logic, so it should no longer assume the responsibility of “initialization”. The initialization work should be completed in a separate class, which can make the code more testable (that is, better write unit tests)
It can be rewritten as follows:
class MiNiCarStore(CarStore):
def __init__(self):
self.carFactory = None
def createCar(self, typeName):
self.carFactory = CarFactory()
return self.carFactory.createCar(typeName)
You can also click Settings – > editor -> inspections -> Python uncheck prompt
Similar Posts:
- [Solved] intellij IdeaCould not autowire. No beans of xxx
- Python: __ new__ Method and the processing of typeerror: object() takes no parameters
- Python Typeerror: super() takes at least 1 argument (0 given) error in Python
- [design and development] Python learning notes – Super () argument 1 must be type, not classobj
- How to Solve vuetify dialog box cannot be edited (The embedded tinymic component)
- [LeetCode] 291. Word Pattern II
- Eclipse CDT launch failed.Binary not found [How to Solve]
- [Solved] Unity Xcode Error: A build only device cannot be used to run this target
- When running Python script, an error is reported: a true sslcontext object is not available
- ModuleNotFoundError No module named urllib2