All stacks / Intents / Type checking Also written as check type
Type Checking at Runtime PY · Variables & Types syntax Copy
type (obj)
isinstance(obj, ClassName)example Copy
score = 95.5
print (type (score))
print (isinstance(score, (int , float)))output
<class 'float'>
TrueNote Prefer isinstance() over type() == because isinstance handles subclasses correctly. The second arg can be a tuple of types.
syntax Copy
from typing import Optional, Union, TypeVar, Genericexample Copy
from typing import TypeVar, Generic
T = TypeVar("T" )
class Stack(Generic[T]):
def __init__(self) -> None :
self._items: list[T] = []
def push(self, item: T) -> None :
self._items.append(item)
def pop(self) -> T:
return self._items.pop()
stack = Stack[int ]()
stack.push(42 )
print (stack.pop())output
42Note Since 3.10, use X | Y instead of Union[X, Y]. Since 3.12, use the type parameter syntax: class Stack[T]: instead of TypeVar + Generic.
typeof Operator JS · Data Types syntax Copy
typeof valueexample Copy
console.log(typeof "hello" );
console.log(typeof 42 );
console.log(typeof true );
console.log(typeof undefined);
console.log(typeof null );
console.log(typeof []);
console.log(typeof (() => {})); Note typeof null === "object" is a historic bug that will never be fixed. Use value === null for null checks. Arrays report as "object" -- use Array.isArray() instead.
Related tasks