i don't understand this python class

DL Neil PythonList at DancesWithMice.info
Mon Jun 29 17:08:16 EDT 2020


On 30/06/20 5:14 AM, joseph pareti wrote:
> I have piece of code with constructs as follows:
> 
> *class* *SentimentNetwork**:*
> 
>      *def* __init__*(*self*,* reviews*,* labels*,* hidden_nodes *=* 10*,*
> learning_rate *=* 0.1*):*
>          np*.*random*.*seed*(*1*)*
>         self*.*init_network*(**len**(*self*.*review_vocab*),*hidden_nodes*,*
> 1*,* learning_rate*)*
> 
>      *def* init_network*(*self*,* input_nodes*,* hidden_nodes*,* output_nodes
> *,* learning_rate*):*
>          # Store the number of nodes in input, hidden, and output layers.
>          self*.*input_nodes *=* input_nodes
>          self*.*hidden_nodes *=* hidden_nodes
>          self*.*output_nodes *=* output_nodes
> 
> which makes me think about the redundant usage of* init_network:*
> 
>     1. as a method, AND
>     2. as a property


Let's imagine this code-functionality, but where all of the 
initialisation is performed in the single __init__ method:-

- there would be four lines of code instead of eight, saving typing-time 
and reading-time
- when reading the code, we would not have to 'jump' our eyes from 
__init__() to init_network, just to see what happens at instantiation

So, why bother?
(is that the nub of your question?)

Consider now the pertinent four lines:

- seed the PRNG (Pseudo-Random Number Generator)
- set number of input_nodes
- set number of hidden_nodes
- set number of output_notes

per my favorite (hi-brown and highly-technical) TV program(me): "which 
of these is not like the others?"

Add to such thinking, that there is apparently much more in the 
initialiser than illustrated (presumably for brevity - thanks!), and you 
can comprehend that there are several *different* functions that all 
have to happen upon instantiation.

So, although init_network() as a separate method is *functionally* 
unnecessary, by separating those tasks from 'everything else that has to 
happen' (and presumably also bunching groups of those tasks together 
under applicable headings!) is a very helpful *documentation* technique 
- in fact, I put it to you, that the name of the method is more helpful 
to a reader, than is the explanatory comment! Your thoughts?

(which #comment 'should' be a docstring, BTW)


The word "property" may have a slightly different definition in Python 
than in other programming languages you have used. If that part of the 
question hasn't been adequately-covered (albeit en-passant) please 
explain further...


Finally, please be aware that there is a Python-Tutor Discussion List 
for the pleasure of both learners and tutors.
-- 
Regards =dn


More information about the Python-list mailing list