Modify

Ticket #82 (new bug)

Opened 10 years ago

Last modified 8 years ago

Code cleanup: Class variable declaration

Reported by: Kaydeth_parpg Owned by: parpg
Priority: minor Milestone: Defect backlog
Component: scripts Version: trunk
Keywords: Cc:
Blocked By: Blocking:
Department:

Description (last modified by Kaydeth_parpg) (diff)

1) Declaring variables inside methods as instance variables unnecessarily, causing them to have a public scope:

Code:

## Class Definition ## class MyClass1a:

def init(self):

print "Constructor for MyClass1a!"

def badMethod(self):

self.publicVar = "bad!" #Instance variable never used outside of this method return self.publicVar

## Object Instantiation ## myC1a = MyClass1a() myC1a.publicVar #Variable has unnecessary public access and has never been initialized, will cause an AttributeError? exception

In the above example, the method badMethod() creates an instance variable self.publicVar. The freedom that Python gives can also be a curse. Since every instance variable is public in Python, publicVar could easily be accessed outside of that method directly. The problem that this causes is if the badMethod() is never called, then publicVar's value is never initialized, which would cause an AttributeError? exception if it's referenced directly. This is the most common issue that I've seen so far. Since the variable is never used outside of this method, I suggest using a plain old local variable like the following:

See http://forums.parpg.net/index.php?topic=400.0 for more info.

The code should be scrubbed to remove these deficiencies.

Attachments

hud.py_code_cleanup.diff (4.5 KB) - added by Atomic 10 years ago.
Removes unncessary public access from method variables, and provides defaut values for instance variables that were originally initialized outside of the constructor

Change History

comment:1 Changed 10 years ago by Kaydeth_parpg

  • Description modified (diff)

Changed 10 years ago by Atomic

Removes unncessary public access from method variables, and provides defaut values for instance variables that were originally initialized outside of the constructor

comment:2 Changed 8 years ago by barra_parpg

  • Milestone changed from Techdemo 3 to Product backlog

moved techdemo 3 tickets to product backlog as we haven't agreed upon sprint goals for techdemo 3 yet

comment:3 Changed 8 years ago by barra_parpg

  • Milestone changed from Product backlog to Bugs

moved bugs to new bug milestone

View

Add a comment

Modify Ticket

Action
as new
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.