GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python type Function - Complete Guide Python type FunctionLast modified April 11, 2025
This comprehensive guide explores Python's Basic Definitions
The
Key characteristics: returns a type object (same as Basic Type Checking
Here's simple usage showing how basic_type.py
# Built-in types
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("hello")) # <class 'str'>
print(type([1, 2, 3])) # <class 'list'>
print(type(True)) # <class 'bool'>
# Custom class
class MyClass:
pass
obj = MyClass()
print(type(obj)) # <class '__main__.MyClass'>
This example shows
For custom classes, the module name ( Type Comparison
type_comparison.py
value = 3.14
# Exact type checking
if type(value) == float:
print("It's a float") # Prints
# Not recommended for inheritance cases
class Parent: pass
class Child(Parent): pass
obj = Child()
print(type(obj) == Parent) # False
print(isinstance(obj, Parent)) # True
This shows
Use Dynamic Class Creation
The three-argument form of dynamic_class.py
# Equivalent to: class Person: pass
Person = type('Person', (), {})
# With attributes and methods
def greet(self):
return f"Hello, {self.name}"
Person = type('Person', (), {
'__init__': lambda self, name: setattr(self, 'name', name),
'greet': greet
})
p = Person("Alice")
print(p.greet()) # Hello, Alice
print(type(p)) # <class '__main__.Person'>
This demonstrates dynamic class creation. The three arguments are: class name, base classes tuple, and namespace dictionary containing attributes/methods.
This is how Python's Metaclass Usage
metaclass.py
class Meta(type):
def __new__(cls, name, bases, namespace):
print(f"Creating class {name}")
return super().__new__(cls, name, bases, namespace)
class MyClass(metaclass=Meta):
pass
# Output: Creating class MyClass
print(type(MyClass)) # <class '__main__.Meta'>
This shows a custom metaclass inheriting from Metaclasses are advanced Python features that allow deep customization of class creation behavior. Type vs isinstance
This example contrasts type_vs_isinstance.py
class Animal: pass class Dog(Animal): pass d = Dog() # type checks exact class print(type(d) == Dog) # True print(type(d) == Animal) # False # isinstance checks inheritance print(isinstance(d, Dog)) # True print(isinstance(d, Animal)) # True # Checking against multiple types print(isinstance(d, (Dog, str, list))) # True
Generally prefer Best Practices
Source ReferencesAuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |