Muhammad Ali Qadri - 9 months ago 92

C++ Question

I would like to know how would I use similar code to **template < typename T>** in python, as it is used in the C++ code example:

`template <typename T>`

unsigned int counting_bit(unsigned int v){

v = v - ((v >> 1) & (T)~(T)0/3); // temp

v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3); // temp

v = (v + (v >> 4)) & (T)~(T)0/255*15; // temp

return v;

}

How would I typecast objects with variable typename in python the same way as it is mentioned in C++?

Answer

Just pass the type to the function.

For example, see this (useless) function:

```
def converter(x, required_type):
return required_type(x)
converter('1', int)
converter('1', float)
```