6 examples of 'how to typecast in python' in Python

Every line of 'how to typecast in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
478def try_cast(to_type):
479 try:
480 return to_type(val)
481 except ValueError:
482 return None
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

40def custom_type_cast(val):
41 return val
321def cast(self, value: Optional[object]) -> object:
322 if value is True:
323 return "true"
324 if value is False:
325 return "false"
326 return super().cast(value)
150def type_convert(obj):
151 """Map Python objects to the corresponding Opendap types.
152
153 Returns the DAP representation of the type as a string.
154
155 """
156 if isinstance(obj, float):
157 return 'Float64'
158 elif isinstance(obj, integer_types):
159 return 'Int32'
160 else:
161 return 'String'
60def _Convert(self, t):
61 module, name = self._GetModuleAndName(t)
62 if not module and name == "None":
63 # PEP 484 allows "None" as an abbreviation of "NoneType".
64 return pytd.NamedType("NoneType")
65 elif self._IsTyping(module):
66 if name in PEP484_CAPITALIZED:
67 return pytd.NamedType(name.lower()) # "typing.List" -> "list" etc.
68 elif name == "Any":
69 return pytd.AnythingType()
70 else:
71 # IO, Callable, etc. (I.e., names in typing we leave alone)
72 return t
73 else:
74 return t
61def pythonise_type(t):
62 t = types.get(t, t)
63 t = types.get(t, t) # because can be XX -> XXDummy -> uint32_t
64 if t in ("float", "double"):
65 return "float"
66 elif t in ("int32_t", "int64_t", "uint32_t", "uint64_t"):
67 return "int"
68 elif t.endswith("_t"):
69 return t[:-2]
70 elif t.startswith("WGPU"):
71 return t[4:]
72 else:
73 return t

Related snippets