Pretty sure I know the answer to this question, but wanted to ask the python community about that.
I'm working on a python project where the current trend is to cut the variable name or the class name to pass it as parameters in other methods... Like :
myVar.__classname__[6:]
or worst :
try :
...
except Error as err :
myVar = err.message.split(':')[0]
getattr(myInst, myVar)
etc... that said, we have to respect a really strict naming convention that will fit with all those horrible line of codes, but I was wondering if that was a common practice and that I was completely out of the line or if I was correct to say that this is just... awful.
It seems like a very bad idea to me, but as we have no choice...
Thanks for any response that would help me
Edit : This is the actual code in the try/except if you would like to have more details :
except ValueError as err:
field_error = self.klass.__name__ + '_' + err.message.split(':')[0]
getattr(self.ui, field_error).setText(err.message)
Edit : So as some asked for more details :
In the example above, We wanna set an error message in a field when value entered by user is wrong.
self.klass represent an SQL Alchemy class, and the "Naming convention" says that every field should start with the SQL Alchemy class name in front of it, then an underscore, and then the field where we set the error message ( currently the same that the user entered wrong ).
This will "construct" ( Oh god... this feels so bad ) the name of the ui field that is wrong, then we'll get it with the getattr on the general ui.
This feels very wrong as it is based on a naming convention that might have billions of exceptions when we'll start to have more classes... Actually, I wouldn't mind fixing this, but the entire project is based on this naming convention. The first example is based on the fact that we have different ui files for our app, and these are indexed by a code ( like 7CDGR01 ). These are then completed by a class to add behaviour ( signal processing... etc ). If I keep the same example, the class is named : Screen7CDGR01. Therefore, to have the code of the screen, you get the end of the classname, from the 6th caracter... then send it to another method etc, etc...
Think you all got it, this is not what I voted for, I think this is just bad, I'm no expert in python, but I think that even if python allows us to do a lot, it should not be used like that.