Global variables from a class

K

Kless

I usually use a class to access to global variables. So, which would
be the correct way to set them --since the following classes--:

--------------------
class Foo:
var = 'lala'

class Bar:
def __init__(self):
self.var = 'lele'
 
P

Piet van Oostrum

Kless said:
K> I usually use a class to access to global variables. So, which would
K> be the correct way to set them --since the following classes--:
K> --------------------
K> class Foo:
K> var = 'lala'
K> class Bar:
K> def __init__(self):
K> self.var = 'lele'
K> --------------------
K> Or is it the same?

I don't see any global variable in your code.
var is a class variable (attribute) in Foo, whereas in Bar var is an
instance variable (attribute). These are two different things. However a
class variable acts as a kind of default for an instance variable. If
all your instances have the same value for var, Foo is a good way to
accomplish this. If they are all different, the Bar is the way to go.

You can also mix them if several of the instances use the same value
'lala' but some need 'lele' or sometime later in the life of the instance
the value will be changed to 'lele'

class Bletch:
var = 'lala'

def update(self):
self.var = 'lele'

In this case it is a matter of taste whether you use the Foo or the Bar
way.
 
S

Steven D'Aprano

Hello,

First thing is a class variable (one for every instance) and second one
an instance variable (one per instance).

One of these things don't belong:

A string variable is a variable holding a string.
A float variable is a variable holding a float.
An int variable is a variable holding an int.
A list variable is a variable holding a list.
A "class variable" is an attribute of a class object, holding an
object of arbitrary type, which is shared by all instances
of the class.

Please don't use the term "class variable" to mean class attribute.
 
J

Jean-Michel Pichavant

Kless said:
I usually use a class to access to global variables. So, which would
be the correct way to set them --since the following classes--:

--------------------
class Foo:
var = 'lala'

class Bar:
def __init__(self):
self.var = 'lele'

This form is the most suited for what your doing:
class Foo:
var = 'lala'

It is a good practice to place your globals into a class (making them non global by the way). It helps also writing good documentation in docstrings.

I would add:
class Foo:
"""Hold the secrets of eternity"""
var = 'lala'
"""One variable"""
ANY_CONSTANT = 14
"""The universal answer to all questions"""

Having strong naming convention also helps a lot.

Jean-Michel

PS: FYI, in the second form, var is an instance variable, and you need to create an instance to access it => Bar().var while Foo.var is enough for the first form.
 
D

Dave Angel

Kless said:
I usually use a class to access to global variables. So, which would
be the correct way to set them --since the following classes--:

--------------------
class Foo:
var = 'lala'

class Bar:
def __init__(self):
self.var = 'lele'
What Python calls global variables are attributes of a module. So other
modules can access them by using the module name, eg.. globalModule.var

If you're trying to make them independent of file name, so others can
access them without knowing what module they are defined in, you can
create a class Foo approach. Then somehow (there are several easy ways)
you get a reference to that class into each module, and they can use
Foo.var to access the attributes of the class. You still have a
reserved name the other modules need to know, it just doesn't have to be
a module itself. And if the data is defined in the initial script, it
avoids the traps that re-importing the script can cause.

If you pass that Foo as a parameter to your functions, you can avoid
having Foo being a global variable itself, in each module other than the
first.

But, to the core of the matter, if there's any chance that the value
might change in the lifetime of the running program, you should probably
not be using them as globals at all. This is where the Bar approach
helps. Attributes defined in that way are attributes of an instance of
Bar, and thus there can be more than one such. The Bar instance can
frequently allow you to generalize a program which might otherwise be
hemmed in by globals. And instead of passing Foo to functions, you pass
bar_instance
 
J

Javier Collado

You're right. I agree on that it's important to use proper words.
Thanks for the correction.

Best regards,
Javier
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top