Main Content
Defining an object that is a modified standard type in Python Archive - Originally posted on "The Horse's Mouth" - 2016-11-02 05:40:57 - Graham Ellis
If you want an object that behaves like a regular standard object but with the odd exception, you can define your own class which inherits from the system type. For example, we have redefined a dict as a specialDict in an example [here] where we've overridden just the __str__ method so that when we print out the object, we get the key/value pairs in a column, sorted by the value.
special class definition:
class specialDict(dict):
def __str__(this):
rzt = ""
sdk = sorted(list(this.keys()),key=lambda x: this[x])
for name in sdk:
rzt += "{1:} - {0:}\n".format(name,this[name])
return rzt
and the only difference in use (so easy to port to existing code) is the need to call a different constructor:
counter = specialDict()
rather than
counter = dict()
or
counter = {}
Some other articles
Y112 - Objects - Intermediate Nesting decorators This article with in Python - examples of use, and of defining your own context Object and Static methods - what is the difference; example in Python 3 Setting up and tearing down with the Python with keyword Deciding whether to use parameters, conditional statements or subclasses Spike solution, refactoring into encapsulated object methods - good design practise A good example of recursion - a real use in Python Changing what operators do on objects - a comparison across different programming languages Object factories in C++, Python, PHP and Perl Python base and inherited classes, test harness and unit testing - new examples Python Properties - how and why Really Simple Class and Inheritance example in Python Inheritance, Composition and Associated objects - when to use which - Python example Backquote, backtic, str and repr in Python - conversion object to string Metaclasses (Python) and Metatables (Lua) Static variables in functions - and better ways using objects A demonstration of how many Python facilities work together A list of special method and attribute names in Python Python - some common questions answered in code examples Defining static methods in Python Should Python classes each be in their own file? The Light bulb moment when people see how Object Orientation works in real use Python decorators - your own, staticmethod and classmethod Mixins example in Python Multiple inheritance in Python - complete working example The Multiple Inheritance Conundrum, interfaces and mixins Methods that run on classes (static methods) in Python How do I set up a constant in Python? TypeError: super() argument 1 must be type, not classobj (Python) Python - fresh examples of all the fundamentals Calling base class constructors Equality, sameness and identity - Python Using a utility method to construct objects of different types - Python Python - formatting objects What are factory and singleton classes? __new__ v __init__ - python constructor alternatives? Practical polymorphism in action Pieces of Python Comparison of Object Oriented Philosophy - Python, Java, C++, Perl Think about your design even if you don't use full UML Class, static and unbound variables Overloading of operators on standard objects in Python Using a Python dictionary as a holder of object attributes Y116 - Applying OO design techniques and best practise This article How to avoid too many recalculations within an object We not only teach PHP and Python - we teach good PHP and Python Practice! Really Simple Class and Inheritance example in Python Inheritance, Composition and Associated objects - when to use which - Python example Tips for writing a test program (Ruby / Python / Java) Plan your application before you start How do I set up a constant in Python? Testing code in Python - doctest, unittest and others Alpaca Case or Camel Case Good Programming practise - where to initialise variables Code quality counts Build on what you already have with OO Python - block insets help with documentation Think about your design even if you don't use full UML Code and code maintainance efficiency