Skip to content

Commit

Permalink
Python: simplify return statements in instance getters
Browse files Browse the repository at this point in the history
  • Loading branch information
generalmimon committed Apr 13, 2022
1 parent b5a0e6a commit 5773e73
Showing 1 changed file with 3 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -454,15 +454,14 @@ class PythonCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
override def instanceCheckCacheAndReturn(instName: InstanceIdentifier, dataType: DataType): Unit = {
out.puts(s"if hasattr(self, '${idToStr(instName)}'):")
out.inc
instanceReturn(instName, dataType)
out.puts(s"return ${privateMemberName(instName)}")
out.dec
out.puts
}

override def instanceReturn(instName: InstanceIdentifier, attrType: DataType): Unit = {
// not very efficient, probably should be some other way to do that, but for now it will do:
// workaround to avoid Python generating an "AttributeError: instance has no attribute"
out.puts(s"return ${privateMemberName(instName)} if hasattr(self, '${idToStr(instName)}') else None")
// workaround to avoid Python raising an "AttributeError: instance has no attribute"
out.puts(s"return getattr(self, '${idToStr(instName)}', None)")
}

override def enumDeclaration(curClass: String, enumName: String, enumColl: Seq[(Long, String)]): Unit = {
Expand Down

0 comments on commit 5773e73

Please sign in to comment.