YouTip LogoYouTip

Python Func Type

# Python2.x Python type() Function [![Image 3: Python Built-in Functions](#) Python Built-in Functions](#) * * * ## Description The type() function returns the type of the object if only the first argument is provided. With three arguments, it returns a new type object. > Difference between isinstance() and type(): > > > * type() does not consider a subclass to be a type of its parent class, ignoring inheritance relationships. > > * isinstance() considers a subclass to be a type of its parent class, taking inheritance relationships into account. > > > > To check if two types are the same, it is recommended to use isinstance(). ### Syntax Here is the syntax for the type() method: type(object) type(name, bases, dict) ### Parameters * name -- The name of the class. * bases -- A tuple of base classes. * dict -- A dictionary containing the namespace defined within the class. ### Return Value With one argument, it returns the object's type. With three arguments, it returns a new type object. * * * ## Examples The following examples demonstrate the use of the type function: # One argument example>>>type(1)>>>type('')>>>type()>>>type({0:'zero'})>>>x = 1>>>type(x) == int# Check if types are equal True# Three arguments>>>class X(object): ... a = 1 ... >>>X = type('X', (object,), dict(a=1))# Create a new type X>>>X ## Difference between type() and isinstance(): class A: pass class B(A): pass isinstance(A(), A)# returns True type(A()) == A# returns True isinstance(B(), A)# returns True type(B()) == A# returns False [![Image 4: Python Built-in Functions](#) Python Built-in Functions](#)
← Python Func PrintPython Func Exec β†’