Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Global variables from a class

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


jonas.esp at googlemail

May 29, 2009, 2:34 AM

Post #1 of 7 (1747 views)
Permalink
Global variables from a class

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'
--------------------

Or is it the same?
--
http://mail.python.org/mailman/listinfo/python-list


piet at cs

May 29, 2009, 2:58 AM

Post #2 of 7 (1696 views)
Permalink
Re: Global variables from a class [In reply to]

>>>>> Kless <jonas.esp [at] googlemail> (K) wrote:

>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.

--
Piet van Oostrum <piet [at] cs>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet [at] vanoostrum
--
http://mail.python.org/mailman/listinfo/python-list


javier.collado at gmail

May 29, 2009, 3:04 AM

Post #3 of 7 (1693 views)
Permalink
Re: Global variables from a class [In reply to]

Hello,

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

For further information, please take a look at:
http://diveintopython.org/object_oriented_framework/class_attributes.html

Best regards,
Javier

2009/5/29 Kless <jonas.esp [at] googlemail>:
> 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'
> --------------------
>
> Or is it the same?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


steve at REMOVE-THIS-cybersource

May 29, 2009, 5:42 AM

Post #4 of 7 (1689 views)
Permalink
Re: Global variables from a class [In reply to]

On Fri, 29 May 2009 12:04:53 +0200, Javier Collado wrote:

> 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.



--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


jeanmichel at sequans

May 29, 2009, 5:55 AM

Post #5 of 7 (1689 views)
Permalink
Re: Global variables from a class [In reply to]

Kless wrote:
> 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'
> --------------------
>
> Or is it the same?
>

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.

--
http://mail.python.org/mailman/listinfo/python-list


davea at ieee

May 29, 2009, 6:00 AM

Post #6 of 7 (1691 views)
Permalink
Re: Global variables from a class [In reply to]

Kless wrote:
> 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'
> --------------------
>
> Or is it the same?
>
>
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



--
http://mail.python.org/mailman/listinfo/python-list


javier.collado at gmail

May 29, 2009, 6:30 AM

Post #7 of 7 (1698 views)
Permalink
Re: Global variables from a class [In reply to]

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

Best regards,
Javier

2009/5/29 Steven D'Aprano <steve [at] remove-this-cybersource>:
> On Fri, 29 May 2009 12:04:53 +0200, Javier Collado wrote:
>
>> 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.
>
>
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.