473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Class level variables in Python

I am just starting to learn the OO side of Python scripting, and I am
a little confused on the following. Take the following example class:
class rectangle(object): z = 1
def __init__(self):
self.x = 2
r = rectangle()
print r.z 1 print r.x 2 r.z = 16
print r.z 16 r.x = 17
print r.x 17


I was wondering if someone could explain if there is any difference
between initalizing your object attributes up in the __init__
constructor or setting them up as (I am guessing at the name here)
object level variables (like z)

thanks

-- brian
Jul 18 '05 #1
3 2462

"Brian Munroe" <bm*****@tribador.nu> wrote in message
news:c1**************************@posting.google.c om...
I am just starting to learn the OO side of Python scripting, and I am a little confused on the following. Take the following example class:
class rectangle(object): z = 1
def __init__(self):
self.x = 2
r = rectangle()
print r.z 1 print r.x 2 r.z = 16
print r.z 16 r.x = 17
print r.x 17


I was wondering if someone could explain if there is any difference
between initalizing your object attributes up in the __init__
constructor or setting them up as (I am guessing at the name here)
object level variables (like z)


Everything set at 'top' level under the class statement is a class
attribute. Ditto for anything set outside the class statement as
someclass.attribute. This include instance methods, which are common
to all instances and therefore *attributes* of the class.

Everything set within instance methods as self.attribute or outside as
someinstance.attribute are instance attributes private to that
instance. Just as a function can have a private local variable with
the same name as a 'public' global variable, an instance can have an
attribute of the same name as an attribute of its class. Just as
function locals 'mask' the global of the same name, instance 'locals'
usually* mask the class attribute of the same name.

In your example above, you start with class attribute z and later add
an r instance attribute of same name (but different value). First you
see one, then the other.

(* I believe the masking exception alluded to above has something to
do with special methods, descriptors, and classes derived from
builtins, but I do not know the current rule will enough to even quote
it. But beginners usually need not worry about it.)

Terry J. Reedy
Jul 18 '05 #2
"Brian Munroe" <bm*****@tribador.nu> wrote in message
news:c1**************************@posting.google.c om...
I was wondering if someone could explain if there is any difference
between initalizing your object attributes up in the __init__
constructor or setting them up as (I am guessing at the name here)
object level variables (like z)


Hi.
Yes there is a difference. One is an instance attribute, the other is a
class attribute:
class C: .... attr = 1
.... def __init__(self):
.... self.attr = 2
.... c = C()
print c.attr 2 print c.__class__.attr 1


HTH
Sean
Jul 18 '05 #3
On Wed, Aug 27, 2003 at 04:43:03PM -0700, Brian Munroe wrote:
I was wondering if someone could explain if there is any difference
between initalizing your object attributes up in the __init__
constructor or setting them up as (I am guessing at the name here)
object level variables (like z)


Generally, if something is more or less constant, I make it a class
variable. If its value depends on the arguments passed to __init__, or
if it is something like a network connection, file operation, etc, then
it goes in __init__.

class Foo:
specialSequence = "blahblah"

def __init__(self, filename):
self.fp = file(filename, "r")
if self.fp.read(8) == self.specialSequence:
# .. do something ..

specialSequence has no reason to be in the constructor, but fp does. I
suppose that's a good guideline - if you can't think of a reason for it
to be in __init__, then don't put it there.

--
m a c k s t a n n mack @ incise.org http://incise.org
The four building blocks of the universe are fire, water, gravel and vinyl.
-- Dave Barry

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: svilen | last post by:
hi. this was named but it is misleading. i want to have the order of setting items in intrinsic dicts (keyword args, or class attributes, etc). which is, a prdered dict trhat matches the...
34
by: SeeBelow | last post by:
I see the value of a class when two or more instances will be created, but Python programmers regularly use a class when there will only be one instance. What is the benefit of this? It has a...
4
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned...
3
by: kman3048 | last post by:
Hello, as a relative newcomer to Python API programming I've got a problem: To extend Python: - there is an API C call to create a module - there is also a API C call to create a method -...
2
by: Ewald R. de Wit | last post by:
I'm running into a something unexpected for a new-style class that has both a class attribute and __slots__ defined. If the name of the class attribute also exists in __slots__, Python throws an...
32
by: Matias Jansson | last post by:
I come from a background of Java and C# where it is common practise to have one class per file in the file/project structure. As I have understood it, it is more common practice to have many...
4
by: chandu | last post by:
Hello, declaring class level variables is good approach or not in c#. in C++ we used prefer diclaring class level variables more than local variables. i had a discussion with somebody that...
1
by: neoone | last post by:
Hi, I reported a bug to the bugtracker (issue 1443), but it was rejected with the comment: "Go ask on c.l.py why this is not a bug" After decrypting c.l.py to the name of this group, I'll do...
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.