473,387 Members | 1,834 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,387 software developers and data experts.

self.var hmm?

class TaskGroup:
def __init__(self):
self.group = []

def addTask(self, task):
self.group.append(task)
is this wrong? i have a program thats too big to post but should i
just do group.append in addTask?

reason is when later doing TaskGroup.group i get None

Jun 27 '08 #1
2 1133
Hi,

that's because

self.group is not the same as TaskGroup.group

quickish:

class TaskGroup
group = []
def __init__(self):
"""
## note that all TaskGroup instances now use the the same self.group
"""
self.group = TaskGroup.group
def addTask(self, task):
self.group.append(task)

hth
martin

On Tue, May 20, 2008 at 10:58 PM, <no**********@yahoo.sewrote:
class TaskGroup:
def __init__(self):
self.group = []

def addTask(self, task):
self.group.append(task)
is this wrong? i have a program thats too big to post but should i
just do group.append in addTask?

reason is when later doing TaskGroup.group i get None

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


--
http://www.xing.com/profile/Martin_Marcher

You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.
Jun 27 '08 #2
On 20 Mai, 22:58, notnorweg...@yahoo.se wrote:
class TaskGroup:
def __init__(self):
self.group = []

def addTask(self, task):
self.group.append(task)

is this wrong? i have a program thats too big to post but should i
just do group.append in addTask?
No, you have to do self.group.append, since group isn't a local
variable in the addTask method, and I doubt that you'd want addTask to
make changes to any global variable called group, which is what might
happen otherwise - that could be a source of potential problems, so
you need to be aware of that!
reason is when later doing TaskGroup.group i get None
TaskGroup.group would be referencing a class attribute called group,
which is "shared" by all TaskGroup instances (and the TaskGroup class
itself, of course). What you need to do is to access the instance
attribute called group via each instance, and the self.group notation
does exactly that: self is the reference to a particular instance (in
the addTask method, it's the instance used to call the addTask
method).

You might wonder why Python doesn't know inside a method that group is
a reference to an instance attribute of that name. The reason,
somewhat simplified, is that Python only has assignments to populate
namespaces (such as the contents of an instance), whereas languages
which employ declarations (such as Java) would have you list the
instance attributes up front. In these other languages, the compiler/
interpreter would know that you're executing a method belonging to an
instance of a particular class, and it would also know what names to
expect as references to class and instance attributes.

Anyway, I hope this makes more sense now than it did a few moments
ago. :-)

Paul
Jun 27 '08 #3

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

Similar topics

2
by: Marc | last post by:
Hi all, I was using Tkinter.IntVar() to store values from a large list of parts that I pulled from a list. This is the code to initialize the instances: def initVariables(self): self.e =...
15
by: Ralf W. Grosse-Kunstleve | last post by:
****************************************************************************** This posting is also available in HTML format: http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html...
18
by: Ralf W. Grosse-Kunstleve | last post by:
My initial proposal (http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html) didn't exactly get a warm welcome... And Now for Something Completely Different: class autoinit(object):...
4
by: David Coffin | last post by:
I'd like to subclass int to support list access, treating the integer as if it were a list of bits. Assigning bits to particular indices involves changing the value of the integer itself, but...
4
by: marek.rocki | last post by:
First of all, please don't flame me immediately. I did browse archives and didn't see any solution to my problem. Assume I want to add a method to an object at runtime. Yes, to an object, not a...
7
by: Andrew Robert | last post by:
Hi Everyone, I am having a problem with a class and hope you can help. When I try to use the class listed below, I get the statement that self is not defined. test=TriggerMessage(data) var...
24
by: Peter Maas | last post by:
The Python FAQ 1.4.5 gives 3 reasons for explicit self (condensed version): 1. Instance variables can be easily distinguished from local variables. 2. A method from a particular class can be...
84
by: braver | last post by:
Is there any trick to get rid of having to type the annoying, character-eating "self." prefix everywhere in a class? Sometimes I avoid OO just not to deal with its verbosity. In fact, I try to...
13
by: Kurda Yon | last post by:
Hi, I found one example which defines the addition of two vectors as a method of a class. It looks like that: class Vector: def __add__(self, other): data = for j in range(len(self.data)):...
6
by: Bart Kastermans | last post by:
I am playing with some trees. In one of the procedures I wrote for this I am trying to change self to a different tree. A tree here has four members (val/type/left/right). I found that self = SS...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.