[Tutor] Concept related to python classes

boB Stepp robertvstepp at gmail.com
Mon Sep 7 17:56:40 EDT 2020


On Mon, Sep 07, 2020 at 10:09:39PM +0100, Alan Gauld via Tutor wrote:
>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.

Aha!  Another misconception inside the boB-brain.  I was aware of
__new__(), but thought that it and __init__() always got called together.
So I was under the misconception that if __init__() got called, then
__new__() would be called first.  So you are saying that when t =
Triangle(a, b, c) happens, first __new__ then __init__ gets called.  But
calling __init__ explicitly from within the class does *not* call __new__.
And reflecting on this I suppose one might explicitly call __new__ without
triggering __init__?

>> 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()

Self-messaging seems natural and intuitive to me.  However, when objects of
one class need to interact with objects of another class, then this is
where I struggle with implementation details.  For example, say I have a
class managing the UI.  The user clicks a "Start Program" button.  Now the
user is supposed to be presented with a math problem which shows up as a
label.  In an entry widget to the right the user is supposed to enter his
answer.  Each problem (of several to many) is an object of a
problem-generating class.  Who instantiates the problems?  Who tells the
problem-generating class to stop sending problems to the UI?  And to
continue this, at the end of the problem solving session, say we want to
save the date, time, type of problem solving session and the percentile
score correct?  Say a file management class is used for this.  Who
instantiates it?  Etc.

As you might suspect the above refers to an actual program that I wrote in
procedural style that I am contemplating OOP-style as I plan on adding
additional problem types (for my son) to solve.  It is these inter-class
communications/instantiation responsibilities that mostly have me pulling
my hair out.

-- 
Wishing you only the best,

boB Stepp


More information about the Tutor mailing list