<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Russ Hensel</title>
    <description>The latest articles on DEV Community by Russ Hensel (@russ_hensel).</description>
    <link>https://dev.to/russ_hensel</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1820014%2Ff0b988a9-ee35-43ee-a027-0f0f8e47fa63.png</url>
      <title>DEV Community: Russ Hensel</title>
      <link>https://dev.to/russ_hensel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/russ_hensel"/>
    <language>en</language>
    <item>
      <title>Do not be Selfish: Use local variables to shadow your instance variables ( Python ).</title>
      <dc:creator>Russ Hensel</dc:creator>
      <pubDate>Mon, 27 Jan 2025 18:42:58 +0000</pubDate>
      <link>https://dev.to/russ_hensel/do-not-be-selfish-use-local-variables-to-shadow-your-instance-variables-python--2le4</link>
      <guid>https://dev.to/russ_hensel/do-not-be-selfish-use-local-variables-to-shadow-your-instance-variables-python--2le4</guid>
      <description>&lt;p&gt;Do not be Selfish: use local variables to shadow your instance variables ( Python ).&lt;/p&gt;

&lt;p&gt;The following applies in many cases with objects.  In this I will focus on a particular case: working with SQL  in QT,  a view and a model.  A single window might have 2 models and 2 views.  Setup code ( with some details left out ) might be something like this: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;code with instance variables&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# the model 1
self.model_1 = QSqlTableModel( ... )
self.model_1.setTable( 'table_1')

# the view 1
self.view_1 = QTableView( )
self.view_1.setModel( self.model_1  )
self.view_1.hideColumn( 0 )
self.view_1.setSelectionBehavior( QTableView.SelectRows )
self.view_1.view.setSortingEnabled( True )

# the model 2
self.model_2 = QSqlTableModel( ... )
self.model_2.setTable( 'table_2')

# the view 2 -- with a mistake
self.view_2 = QTableView( )
self.view_2.setModel( self.model_1  )
self.view_2.hideColumn( 3 )
self.view_2.setSelectionBehavior( QTableView.SelectRows )
self.view_2.setSortingEnabled( False )
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;less selfish code that shadows your instance variables but does exactly the same thing:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# the model 1
self.model_1 = QSqlTableModel( ... )
self.model_1 = model

model.setTable( 'table_1')

# the view 1
view = QTableView( )
self.view_1  = view
view.setModel( self.model_1  )
view.hideColumn( 0 )
view.setSelectionBehavior( QTableView.SelectRows )
view.setSortingEnabled( True )

# the model 2
model = QSqlTableModel( ... )
self.model_2 = model
model.setTable( 'table_2')

# the view 2 -- with a mistake corrected
view = QTableView( )
self.view_2 = view
view.setModel( model  )
view.hideColumn( 3 )
view.setSelectionBehavior( QTableView.SelectRows )
view.setSortingEnabled( False )
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So what do I see that is different, and why do I like it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;self.model_1 mostly becomes model and self.view_1 mostly becomes view.  And so on. ( in this case the shadowing is not exact it would be with self.model…. )&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I have added 4 lines of code to  the self.model_1….. but I have saved many characters of text.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Because of the local variables I can copy and paste the code to other places with many fewer changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;My experiments show that using local variables like this is actually faster than instance variables.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you mistype a local variable that is undefined your IDE will flag it as an error, this does not happen with instance variables because they can be defined very dynamically.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Is this faster coding, with faster running, and fewer bugs?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So what do you think:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;worth a try?
useless?
obvious?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
    </item>
  </channel>
</rss>
