[Tutor] global variables/constants versus volatile variables/constants
Steven D'Aprano
steve at pearwood.info
Sat Jun 14 03:21:38 CEST 2014
On Fri, Jun 13, 2014 at 09:42:34PM +0530, diliup gabadamudalige wrote:
> Thank you all for these elaborate explanations.
> so would it be wrong to assume that,
> 1. the list or dict if 'large' and not mutated is better declared once in a
> Class and used throughout the program?
There is *no* advantage to writing this:
class Foo:
data = ['a', 'large', 'list']
result = function(Foo.data)
instead of this:
data = ['a', 'large', 'list']
result = function(data)
Just dumping things into a class is not very helpful. You should ask
yourself:
* Does my code represent a "thing", like a string, or a web server,
or a customer record? Then a class might be a good solution.
* Does this thing have data and functions that go together? For
example, a customer record might have data like Account Balance,
and a function like update_account_balance. Then a class might
be a good solution.
* Could I have more than one of these "things" at the same time?
For example, you obviously could have more than one string or
customer record. Then a class is probably a good solution.
If none of these things are true, then a class is probably a bad
solution.
> or
> 2. Lists that are read only are better off being tuples?
Possibly. It depends.
As a general rule, tuples may be used for heterogeneous data, lists
should be used for homogeneous data. What do I mean by this?
This is a good use for a tuple:
customer = (name, address, phone_number, status, balance)
You have a fixed number of fields, and each field means something
different (they are hetrogeneous). You should not use a list for
something like this.
This is a good use for a list:
[value, another_value, a_third_value, ...]
There is an arbitrary number of items, and each item represents the same
kind of thing (they are homogeneous). Don't think of types, like
"all the items are floats", but instead think "all the items are a
person's height".
As a micro-optimization, you might treat tuple as a frozen-list. That's
not so unusual that you will confuse people by it, but it is a little
unusual, and a matter of personal taste.
> or
> 3.the list or dict if 'large' and not mutated is better declared once as a
> global variable and used throughout the program?
If it's not mutated, it isn't really a *variable* is it?
But yes, I would consider this the simplest thing that can work. This
would be my preference.
> The variables, lists carrying variables, and dict. carrying variables are
> declared as a class. So why not include all above in the same class?
> Varables and constans which are used throughout the program in one class
> declared beofr the program enters the main executing loop?
> wouldn't this be fast and the memory usage static up to a point (for all
> declared items in the variables class)?
I'm afraid I don't understand this paragraph. Can you try explaining
more carefully please?
(By the way: please trim unnecessary quoted text. Your email contained
dozens of lines of double-quoted > > comments that had nothing to do
with the questions you asked. If the comments aren't relevant, please
delete them. Thank you.)
--
Steven
More information about the Tutor
mailing list