A downside to having the class defined inside the function is that there is no way to reference it from elsewhere. For instance, your other code can't use isinstance
to test if a value is a SearchResult
instance, since they have no way to reference the SearchResult
class.
Another issue is that your return values won't technically be of the same type! That's because each time the search
function is run, it creates its own SearchResult
class that is different from the SearchResult
classes created by each other run. So, if you had two results, a
and b
, type(a) == type(b)
would be false. This isn't likely to matter in most cases, but it might occasionally become awkward.
If the only reason you have for doing things this way is that you don't want the class showing up in your tab-completion list, you might be able to hide it a better way (though exactly how may depend on what IDE you're using and how its tab-completion works). Something you might want to try is prefixing the class name with an underscore (_SearchResult
), which may make the tab-completion ignore it since a leading underscore is conventionally understood to indicate that something is "private".