|
Overloading of operators on standard objects in Python
In python, everything is an object and operations performed by operators such as + and * vary in exactly what they do based on the class (type) or objects on which they're run.
If I use the * operator on a list, it replicates it ... but if I use it on an integer, it multiplies it ...
apple = [17]
orange = 20
firstfruit = apple * 7
secondfruit = orange * 7
print firstfruit
print secondfruit
Results:
[17, 17, 17, 17, 17, 17, 17]
140
(written 2005-07-19, updated 2006-06-05)
Associated topics are indexed under Y104 - Python - Lists and Tuples [3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06) [3257] All possible combinations from a list (Python) or array (Ruby) - (2011-04-23) [3181] Beware - a=a+b and a+=b are different - Python - (2011-02-23) [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02) [2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12) [2719] Traffic lights in Python - (2010-04-13) [2368] Python - fresh examples of all the fundamentals - (2009-08-20) [2284] Strings as collections in Python - (2009-07-12) [2280] Creating and iterating through Python lists - (2009-07-12) [1789] Looking for a value in a list - Python - (2008-09-08) [1641] Tektronix 4010 series / Python Tuples - (2008-05-13) [1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06) [955] Python collections - mutable and imutable - (2006-11-29) [899] Python - extend v append on a list - (2006-10-20) [657] The ternary operator in Python - (2006-03-25)
Y112 - Python - Objects - Intermediate [3524] Metaclasses (Python) and Metatables (Lua) - (2011-11-17) [3472] Static variables in functions - and better ways using objects - (2011-10-10) [3442] A demonstration of how many Python facilities work together - (2011-09-16) [3002] A list of special method and attribute names in Python - (2010-10-17) [2994] Python - some common questions answered in code examples - (2010-10-10) [2905] Defining static methods in Python - (2010-08-05) [2889] Should Python classes each be in their own file? - (2010-07-27) [2785] The Light bulb moment when people see how Object Orientation works in real use - (2010-05-28) [2764] Python decorators - your own, staticmethod and classmethod - (2010-05-14) [2722] Mixins example in Python - (2010-04-14) [2720] Multiple inheritance in Python - complete working example - (2010-04-14) [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11) [2693] Methods that run on classes (static methods) in Python - (2010-03-25) [2485] How do I set up a constant in Python? - (2009-10-31) [2409] TypeError: super() argument 1 must be type, not classobj (Python) - (2009-09-18) [1819] Calling base class constructors - (2008-10-03) [1661] Equality, sameness and identity - Python - (2008-05-31) [1644] Using a utility method to construct objects of different types - Python - (2008-05-17) [1517] Python - formatting objects - (2008-01-24) [1217] What are factory and singleton classes? - (2007-06-04) [1146] __new__ v __init__ - python constructor alternatives? - (2007-04-14) [964] Practical polymorphism in action - (2006-12-04) [903] Pieces of Python - (2006-10-23) [831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13) [656] Think about your design even if you don't use full UML - (2006-03-24) [477] Class, static and unbound variables - (2005-10-25) [296] Using a Python dictionary as a holder of object attributes - (2005-04-30)
Some other Articles
Training course plans for 2006 What is a callback? Feast or famine Managing conflict and disappointment Overloading of operators on standard objects in Python Central London Courses - Perl, PHP, Python, Tcl, MySQL Exceptions in Python Bridging to the customer requirement Getting the lighting right A word of admiration for the London cabbie
|
3603 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|