I’m new to python but I caught on to the basics pretty quick and decided to start trying to make a program while I’m still learning, since I learn best by actually doing things.
So I’m making a program in python that will add polynomials and I need to see if a character from the parser is numeric im using the isdigit() command.
Instead of having to type isdigit() all the time in my code such as n.isdigit(), I want to assign it to a variable t = 'isdigit()' and then type n.t. This doesn’t work, so is there an alternative to not typing the whole command?
While you can start your code with something like
t = str.isdigitand then checkt(n)(you can’t make it an attribute of strings because you can’t intercept attribute lookup from the outside), this is actually a pretty bad idea. While it’s quicker to type, it is significantly harder to read. This will bite you (or anyone else) working the code in a few weeks from now. This is not because abstracting over unnecessary details is bad (it isn’t), this snippet simply isn’t complicated enough to benefit from an abstraction. Any readable name for this is at least as long asisidigt, so you can’t actually win any readability.