Implement __str__()
or __repr__()
in the class’s metaclass.
class MC(type):
def __repr__(self):
return 'Wahaha!'
class C(object):
__metaclass__ = MC
print(C)
Use __str__
if you mean a readable stringification, use __repr__
for unambiguous representations.
Edit: Python 3 Version
class MC(type):
def __repr__(self):
return 'Wahaha!'
class C(object, metaclass=MC):
pass
print(C)
Related Contents:
- Why do Python classes inherit object?
- What is the difference between old style and new style classes in Python?
- What is the difference between __init__ and __call__?
- Does Python have “private” variables in classes?
- How to invoke the super constructor in Python?
- Is it possible to make abstract classes in Python?
- How to create a custom string representation for a class object?
- AttributeError: ‘datetime’ module has no attribute ‘strptime’
- How do I use method overloading in Python?
- How do I correctly setup and teardown for my pytest class with tests?
- What are data classes and how are they different from common classes?
- Python decorators in classes
- putting current class as return type annotation [duplicate]
- class method generates “TypeError: … got multiple values for keyword argument …”
- Why do we use __init__ in Python classes?
- Python class definition syntax
- Should I use a class or dictionary?
- How should I declare default values for instance variables in Python?
- Is it possible to overload Python assignment?
- Prevent creating new attributes outside __init__
- Using super with a class method
- Monkey patching a class in another module in Python
- Saving and loading multiple objects in pickle file?
- Python: How do I make a subclass from a superclass?
- Is it possible to make abstract classes?
- Private members in Python
- Printing all instances of a class
- assigning class variable as default value to class method argument
- How to create a read-only class property in Python? [duplicate]
- Attaching a decorator to all functions within a class
- Passing variables, creating instances, self, The mechanics and usage of classes: need explanation [closed]
- Python calling method in class
- Is there a way to circumvent Python list.append() becoming progressively slower in a loop as the list grows?
- Create object from class in separate file
- Python: deleting a class attribute in a subclass
- Organizing Python classes in modules and/or packages
- Import statement inside class/function definition – is it a good idea?
- Where is the Python documentation for the special methods? (__init__, __new__, __len__, …)
- Error when calling the metaclass bases: function() argument 1 must be code, not str
- overriding bool() for custom class [duplicate]
- Difference between Module and Class in Python
- How would I access variables from one class to another?
- Python – Should I put my helper functions inside or outside the class? [closed]
- Method arguments in Python [duplicate]
- How do you call an instance of a class in Python?
- Invoking a constructor in a ‘with’ statement
- Preventing a class from direct instantiation in Python
- Does Python have class prototypes (or forward declarations)?
- Does Python have something like anonymous inner classes of Java?
- How to dynamically access class properties in Python?