0
mytype = "int" 
myvalue = "35"
my_int_val = mytype(myvalue)

This throws up -

TypeError: 'str' object is not callable

I can't seem to remember the way to do so. Any ideas?

Please note that I have to use "str", "int" instead of str or int (without quotes), because I am getting this value from somewhere else where it's being passed on as a string.

5
  • my_int_val = int(myvalue) Commented Nov 16, 2022 at 16:23
  • you aren't writing a function around myvalue. You are calling a variable on a variable. Try str("35") that works as str is a function and "str" within mytype is a string Commented Nov 16, 2022 at 16:24
  • Errors aside, why would you expect str("35") to return an integer value? Commented Nov 16, 2022 at 16:24
  • @JohnGordon I've corrected my question. I meant to say int instead of str. Commented Nov 16, 2022 at 16:50
  • What you have now is equivalent to my_int_val = "int"("35"). The name of (a reference to) a type is not the same thing as the type itself. As types are first-class values in Python, you can define mytype = int, and the rest of your code will work as you expect. Commented Nov 16, 2022 at 18:26

5 Answers 5

3

If you want to use any builtin function dynamically to convert the data you can fetch it from __builtins__.

mytype = "str"
myvalue = "34"

func = getattr(__builtins__, mytype)

print(func(myvalue))
print(type(func(myvalue)))

This will give you

34
<class 'str'>

If you use mytype = "float" you'll get

34.0
<class 'float'>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - That is how I intend to use it (str, float, int etc)
If you're going to do this, why not just say mytype = str? Then you don't need a lookup at all. You already have the actual str type object.
0
my_int_val = int(myvalue)
my_str_val = str(myvalue)

is the cleanest way to do this. But for some reason if you want the type to be stored in a string and call it, you can use eval:

t = "int"
my_int_val = eval(f"{t}({myvalue})")

1 Comment

I wouldn't use eval until I really need to. In nearly 20 years of Python programming I used it once. A must read: Eval really is dangerous. You shouldn't use eval until you read and unterstood his article and after you read and understood it you don't want to use it anymore.
0

mytype is a variable not a datatype

int_val = int(myvalue)
str_val = str(myvalue)

Comments

0

my try:

mytype = "int"
myvalue = "35"
#my_int_val = myvalue.type(mytype)


my_int_val = eval(mytype)(myvalue)  


print(my_int_val, type(my_int_val))

mytype = "str"
myvalue = 35

print(myvalue, type(myvalue))

my_int_val = eval(mytype)(myvalue)

print(my_int_val, type(my_int_val))

output:

35 <class 'int'>
35 <class 'int'>
35 <class 'str'>

but need to confess I copied from here : Convert string to Python class object?

oops didnt notice answer above, in any case :

Warning: eval() can be used to execute arbitrary Python code. You should never use eval() with untrusted strings. (Security of Python's eval() on untrusted strings?)

Comments

0

Instead of mytype = "str" just do mytype = str and it will work (as it sets mytype to the builtin function str).

Example with a function:

def cast(value, totype):
    return totype(value)

mystr = cast(35, str)
print(mystr, type(mystr))

myfloat = cast("35", float)
print(myfloat, type(myfloat))

Output:

35 <class 'str'>
35.0 <class 'float'>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.