Skip to content
Advertisement

Automatically change variable coming into function to string

Is there a way to automatically change the type of a variable when it arrives at a function, eg:

JavaScript

I know this code won’t work, but it’s just to explain my question.

I also know I can just do x = my_func(str(x)), but I specifically want to make sure all variables coming into function will be string.

Advertisement

Answer

The simplest way to solve your problem would be to explicitly convert the input to a string, like so:

JavaScript

If you don’t want to do this explicitly, you could (as suggested in the comments) use a decorator:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement