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.

  • `my_int_val = int(myvalue)` – John Gordon Nov 16 '22 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 – JJFord3 Nov 16 '22 at 16:24
  • Errors aside, why would you expect `str("35")` to return an integer value? – John Gordon Nov 16 '22 at 16:24
  • @JohnGordon I've corrected my question. I meant to say int instead of str. – HappyChappy Nov 16 '22 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. – chepner Nov 16 '22 at 18:26

5 Answers5

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'>
Matthias
  • 12,873
  • 6
  • 42
  • 48
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})")
Priyatham
  • 2,821
  • 1
  • 19
  • 33
  • 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](https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html). 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. – Matthias Nov 16 '22 at 16:35
0

mytype is a variable not a datatype

int_val = int(myvalue)
str_val = str(myvalue)
Harez
  • 30
  • 5
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?)

pippo1980
  • 2,181
  • 3
  • 14
  • 30
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'>
treuss
  • 1,913
  • 1
  • 15