Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Example 1: "better (shorter, encapsulated, more error-proof)"</p> <p>Not really. </p> <ul> <li><p>The example 1 function call is no shorter than example 2; you have to set the instance variables before calling the function instead of passing the values as arguments. It's the same code.</p></li> <li><p>The example 1 function call is no more encapsulated. Encapsulation is a property of the class <em>as a whole</em>, not an individual method. Methods are just methods, and they often have arguments so that they're clear, obvious, replaceable and easy to test.</p></li> <li><p>The example 1 function call is not error-proof in any sense of the word. You're just as likely to forget to set the instance variable as you are to forget to pass the instance variable in the function call. </p> <p>When you have explicit arguments (example 2), Python can check to see that you provided the correct number of arguments. In example 1, there's no checking done for you.</p></li> </ul> <p>Example 1: "harder to test"</p> <p>Agreed.</p> <p><strong>Summary</strong></p> <p>Instance variables are special. They reflect the state of being of some object. For model objects, they're serious business because they're often persistent. For GUI objects, they're "what's being displayed right now" which is often transient.</p> <p>Don't over-use instance variables. Instance variables should be meaningful and significant. Not just junk variables for things that don't have a proper home anywhere else.</p> <p>Method functions are just functions. In an OO world, they're still just functions. They map input arguments to output results (or object state changes). Pass all the arguments that make sense. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload