Objects are a great way to bundle up a whole lot of values (also known as properties or attributes) into a single container / collection, allowing the the programmer who makes use of the objects to be able to set them up just once and delegate the management of the values to the class / object code. So:
trafalgar = square(110,"London, UK")
more code here
print ("circumference of {}m in {}".format(
trafalgar.getEdge(), trafalgar.getCountry()))
sets up an object of class square early on, then referenced it twice without having to pass in values again (and indeed without having to manage those values) later on.
In contrast, if the user were to construct the objects without attributes and pass their values in later, there's likely to be significantly more code to be written in the application for attribute management, and that code will be much more prone to errors and require the application programmer to be much more knoweledgable about the objects as (s)he will need to know what method requires what parameters.
So this is a poor approach!
trafalgar = square()
more code here
print ("circumference of {}m in {}".format(
trafalgar.getEdge(110), trafalgar.getCountry("London, England")))
There's a pedantic school of thought that says that each method should perform a single task, and that a constructor with parameters passed in is both creating and populating an object, and that the setup should be done in separate calls, so it might look like:
trafalgar = square()
trafalgar.setWidth(110)
trafalgar.setLocation("London, Great Britain")
more code here
print ("circumference of {}m in {}".format(
trafalgar.getEdge(), trafalgar.getCountry()))
There's nothing 'wrong' with this approach in that all the attributes are stored in the object like they were in my original code, but it does make for longer applications, and it does mean that object methods really need to check that the population has been done. So, personally for most uses, I would go for create-and-populate constructor, the only exception being if I needed the granularity at setup for some reason. Even then, I would probably use optional and perhaps named parameters, with other methods available to set attributes later on if need be.
Sample code -
top example (best)
Sample code -
middle example (POOR)
Sample code -
lower example (pedantic)
Source code examples all in Python (Python 3).
(written 2016-02-18)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y300 - Python 3 - What is new, what's changed and why [753] Python 3000 - the next generation - (2006-06-09)
[1788] Python 2 to Python 3 / Python 3000 / Py3k - (2008-09-07)
[1791] The road ahead - Python 3 - (2008-09-10)
[2277] Python classes / courses - what version do we train on? - (2009-07-10)
[2285] Great new diagrams for our notes ... Python releases - (2009-07-13)
[2559] Moving the product forward - ours, and MySQL, Perl, PHP and Python too - (2010-01-01)
[2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
[2871] Moving from Python 2.6 to Python 3 - (2010-07-14)
[4469] Sorting in Python 3 - and how it differs from Python 2 sorting - (2015-04-20)
[4470] Testing in Python 3 - unittest, doctest and __name__ == __main__ too. - (2015-04-21)
[4590] Progress on moving from Python 2 to Python 3 - training for both versions - (2015-12-01)
[4649] Object and Static methods - what is the difference; example in Python 3 - (2016-02-17)
[4711] Convering from Python 2 to Python 3 - an update, and the 2to3 utility - (2016-10-30)
[4712] A reminder of the key issues to consider in moving from Python 2 to Python 3 - (2016-10-30)
Q906 - Object Orientation and General technical topics - Object Orientation: Individual Objects [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
[507] Introduction to Object Oriented Programming - (2005-11-27)
[1543] Learning Object Oriented Principles (and perhaps Java) - (2008-02-17)
[1864] Object Oriented Perl - First Steps - (2008-11-01)
[1925] Introduction to Object Oriented Programming - (2008-12-06)
[2171] Cleaning up redundant objects - (2009-05-11)
[2173] Basic OO principles - (2009-05-11)
[2393] A first demonstration of OO, including polymorphism - (2009-09-04)
[2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
[3436] Moving from scripting to Object Orientation in Python - (2011-09-13)
[3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
[4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
[4448] What is the difference between a function and a method? - (2015-03-04)
[4591] From single block to structure and object oriented programming - (2015-12-02)
Y106 - Object Oriented Python [477] Class, static and unbound variables - (2005-10-25)
[834] Python makes University Challenge - (2006-08-15)
[900] Python - function v method - (2006-10-20)
[1306] Python class rattling around - (2007-08-16)
[1348] Screw it or Glue it? Access to Object variables - a warning - (2007-09-12)
[2017] Python - a truly dynamic language - (2009-01-30)
[2169] When should I use OO techniques? - (2009-05-11)
[2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
[3085] Object Oriented Programming for Structured Programmers - conversion training - (2010-12-14)
[3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
[3673] Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26)
[3878] From Structured to Object Oriented Programming. - (2012-10-02)
[3947] this or self - what are they, and what is the difference? (Python) - (2012-12-08)
[4028] Really Simple Class and Inheritance example in Python - (2013-03-04)
[4129] Simple OO demonstration in C++, comparison to Python - (2013-07-01)
[4721] When to check an object type - Python isinstance example - (2016-11-03)
Some other Articles
Getting to the Royal United Hospital - the Hopper and the alternativesCoats of arms - towns and authorities in WiltshireTesting new algorithms in PHPPressure selling in the fire safety businessWhy populate object with values as you construct them?Why is bus funding a much hotter topic than a new set of traffic lights?On the problems of a printed train timetableSometimes you wonder about people. And perhaps you should not.What are callbacks? Why use them? An example in Python