[Tutor] Concept related to python classes

Alan Gauld alan.gauld at yahoo.co.uk
Mon Sep 7 17:09:39 EDT 2020


On 07/09/2020 21:26, boB Stepp wrote:
>
>> Now, a challenge question: the contents of the __init__ method are
>> identical to the contents of the resize method 

> The most natural thing to me is to use either the __init__ method or the
> resize method to solely set the size of the triangle.  

Correct i think thats what mats meant.


> But if the former is used
> then a new object will be created, 

No it won't. init() initialises an object it does not create one.
It is an initializer not a constructor. in Python the constructor
is __new__() You can call init() as often as you like.

But from a semantic point of view it would be better to put the
code that performs the operation in the method corresponding to tat
operation. init's job is to initialize - usually once - but that
can including calling other operations. So I'd posit that
init should call resize.

> def set_size(self, a, b, c):  # "resize" seems an inappropriate name now
>      self.a = a
>      self.b = b
>      self.c = c

You can call it that if you prefer. to me if you change any of
the side sizes you are resizing the triangle but that's purely
subjective. The main point is that we should be striving for
a name that describes the operation on the object not names
that reflect the (current) internal implementation details.

> If one wanted to do input validation, one could individually make a, b, c
> properties with setter methods that do the validation.

Correct, since the validation of the values stops the object state going
inconsistent - eg a negative size - so properties would be appropriate
for those checks.

> This is what *I* am thinking.  What are you thinking here?
> 
> If I am on the right track is calling a method from _init__() to
> massage/validate data a common thing to do in OOP?

Yes, all the time and in any language. In fact most methods
of significant classes call other methods of the same class.
This is known as self messaging. Its particularly common in GUI
Application classes where you often put the widget building code
in one method, the widget initialisation in another (so you
can reset the window values on demand) and then call

self.buildUI()
self.initUI()

from inside init()

This is one advantage of C++ (and to a lesser degree Java, C#
etc) approach with public, protected and private methods.
Only the private methods should actually change the attributes,
the protected methods perform internal logic/calculations by
accessing the private methods, and the public methods mainly
call one or more protected methods. Such an approach leads
to highly flexible code that can be changed without requiring
any changes in the client code. And if the private/protected
methods are put in a separate compilation unit(aka file!) then
changes to that will only require a recompile and link of that
one file the public part never changes.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list