Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Why populate object with values as you construct them?

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
Y106 - Object Oriented Python
  [4721] When to check an object type - Python isinstance example - (2016-11-03)
  [4591] From single block to structure and object oriented programming - (2015-12-02)
  [4448] What is the difference between a function and a method? - (2015-03-04)
  [4129] Simple OO demonstration in C++, comparison to Python - (2013-07-01)
  [4028] Really Simple Class and Inheritance example in Python - (2013-03-04)
  [4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
  [3947] this or self - what are they, and what is the difference? (Python) - (2012-12-08)
  [3878] From Structured to Object Oriented Programming. - (2012-10-02)
  [3673] Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26)
  [3436] Moving from scripting to Object Orientation in Python - (2011-09-13)
  [3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
  [3085] Object Oriented Programming for Structured Programmers - conversion training - (2010-12-14)
  [2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
  [2169] When should I use OO techniques? - (2009-05-11)
  [2017] Python - a truly dynamic language - (2009-01-30)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [1348] Screw it or Glue it? Access to Object variables - a warning - (2007-09-12)
  [1306] Python class rattling around - (2007-08-16)
  [900] Python - function v method - (2006-10-20)
  [834] Python makes University Challenge - (2006-08-15)
  [477] Class, static and unbound variables - (2005-10-25)

Q906 - Object Orientation and General technical topics - Object Orientation: Individual Objects
  [3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
  [2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
  [2393] A first demonstration of OO, including polymorphism - (2009-09-04)
  [2173] Basic OO principles - (2009-05-11)
  [2171] Cleaning up redundant objects - (2009-05-11)
  [1864] Object Oriented Perl - First Steps - (2008-11-01)
  [1543] Learning Object Oriented Principles (and perhaps Java) - (2008-02-17)
  [507] Introduction to Object Oriented Programming - (2005-11-27)
  [227] Bellringing and Programming and Objects and Perl - (2005-02-25)

Y300 - Python 3 - What is new, what's changed and why
  [4712] A reminder of the key issues to consider in moving from Python 2 to Python 3 - (2016-10-30)
  [4711] Convering from Python 2 to Python 3 - an update, and the 2to3 utility - (2016-10-30)
  [4649] Object and Static methods - what is the difference; example in Python 3 - (2016-02-17)
  [4590] Progress on moving from Python 2 to Python 3 - training for both versions - (2015-12-01)
  [4470] Testing in Python 3 - unittest, doctest and __name__ == __main__ too. - (2015-04-21)
  [4469] Sorting in Python 3 - and how it differs from Python 2 sorting - (2015-04-20)
  [2871] Moving from Python 2.6 to Python 3 - (2010-07-14)
  [2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
  [2559] Moving the product forward - ours, and MySQL, Perl, PHP and Python too - (2010-01-01)
  [2285] Great new diagrams for our notes ... Python releases - (2009-07-13)
  [2277] Python classes / courses - what version do we train on? - (2009-07-10)
  [1791] The road ahead - Python 3 - (2008-09-10)
  [1788] Python 2 to Python 3 / Python 3000 / Py3k - (2008-09-07)
  [753] Python 3000 - the next generation - (2006-06-09)


Back to
Object and Static methods - what is the difference; example in Python 3
Previous and next
or
Horse's mouth home
Forward to
Pressure selling in the fire safety business
Some other Articles
Getting to the Royal United Hospital - the Hopper and the alternatives
Coats of arms - towns and authorities in Wiltshire
Testing new algorithms in PHP
Pressure selling in the fire safety business
Why 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 timetable
Sometimes you wonder about people. And perhaps you should not.
What are callbacks? Why use them? An example in Python
4759 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, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4650_Why ... them-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat