Hello,
Learning python from a c++ background. Very confused about this:
============
class jeremy:
list=[]
def additem(self):
self.list.append("hi")
return
temp = jeremy()
temp.additem()
temp.additem()
print temp.list
temp2 = jeremy()
print temp2.list
==============
The output gives:
['hi','hi']
['hi','hi']
Why does adding items to one instance produce items in a separate
instance? Doesn't each instance of jeremy have its' own "list"?
Many thanks for clearing up this newbie confusion.
Jeremy. 9 1275
On 12 jul, 17:23, Jeremy Lynch <jeremy.ly...@gmail.comwrote:
Hello,
Learning python from a c++ background. Very confused about this:
============
class jeremy:
list=[]
def additem(self):
self.list.append("hi")
return
temp = jeremy()
temp.additem()
temp.additem()
print temp.list
temp2 = jeremy()
print temp2.list
==============
The output gives:
['hi','hi']
['hi','hi']
Why does adding items to one instance produce items in a separate
instance? Doesn't each instance of jeremy have its' own "list"?
You've defined list (very bad choice for a name), as a class variable.
To declare instance variable you should have written:
class jeremy:
On Jul 12, 10:23 am, Jeremy Lynch <jeremy.ly...@gmail.comwrote:
Hello,
Learning python from a c++ background. Very confused about this:
============
class jeremy:
list=[]
def additem(self):
self.list.append("hi")
return
temp = jeremy()
temp.additem()
temp.additem()
print temp.list
temp2 = jeremy()
print temp2.list
==============
The output gives:
['hi','hi']
['hi','hi']
Why does adding items to one instance produce items in a separate
instance? Doesn't each instance of jeremy have its' own "list"?
Many thanks for clearing up this newbie confusion.
Jeremy.
The reason it works like that is that your variable "list" isn't an
instance variable per se. Instead, you should have it like this:
<code>
class jeremy:
def __init__(self):
self.lst=[]
def additem(self):
self.lst.append("hi")
return
</code>
Now it works as expected. It's some kind of scope issue, but I can't
explain it adequately.
Mike
On 12 jul, 17:23, Jeremy Lynch <jeremy.ly...@gmail.comwrote:
Hello,
Learning python from a c++ background. Very confused about this:
============
class jeremy:
list=[]
You've defined list (very bad choice of a name, BTW), as a class
variable. To declare is as instance variable you have to prepend it
with "self."
On Jul 12, 6:23 pm, Jeremy Lynch <jeremy.ly...@gmail.comwrote:
Hello,
Learning python from a c++ background. Very confused about this:
============
class jeremy:
list=[]
def additem(self):
self.list.append("hi")
return
temp = jeremy()
temp.additem()
temp.additem()
print temp.list
temp2 = jeremy()
print temp2.list
==============
The output gives:
['hi','hi']
['hi','hi']
Why does adding items to one instance produce items in a separate
instance? Doesn't each instance of jeremy have its' own "list"?
Many thanks for clearing up this newbie confusion.
Jeremy.
You are defining the list in the class context and so it becomes a
class field/member.
For defining instance members you need to always prefix those with
self (this) in the
contexts it is available (f.e. in the instance method context).
bests,
../alex
--
..w( the_mindstorm )p.
Jeremy Lynch wrote:
Hello,
Learning python from a c++ background. Very confused about this:
============
class jeremy:
list=[]
def additem(self):
self.list.append("hi")
return
temp = jeremy()
temp.additem()
temp.additem()
print temp.list
temp2 = jeremy()
print temp2.list
==============
The output gives:
['hi','hi']
['hi','hi']
Why does adding items to one instance produce items in a separate
instance? Doesn't each instance of jeremy have its' own "list"?
Many thanks for clearing up this newbie confusion.
Jeremy.
In this code, "list" (bad name) is a class attribute and all therefor in
all instances, the "list" attribute is reference to the class attribute
unless otherwise assigned, as in __init__.
For instance, try:
temp = jeremy()
temp.additem()
temp.additem()
print temp.list
temp2 = jeremy()
temp2.list = [1,2,3]
print temp.list, temp2.list, jeremy.list
And see which ones look the same (same reference) or look different.
James
Bart Ogryczak wrote:
On 12 jul, 17:23, Jeremy Lynch <jeremy.ly...@gmail.comwrote:
>Hello,
Learning python from a c++ background. Very confused about this:
============ class jeremy: list=[]
You've defined list (very bad choice of a name, BTW), as a class
variable. To declare is as instance variable you have to prepend it
with "self."
Ouch!
'self' is *not* a reserved ord in python, it doesn't do anything. So
just popping 'self' in front of something doesn't bind it to an instance.
Here is how it works:
class Jeremy(object): # you better inherit from 'object' at all times
classlist = [] # class variable
def __init__(self): # "constructor"
self.instancelist = [] # instance variable
def add_item(self, item):
self.instancelist.append(item)
'self' is the customary name for the first argument of any instance
method, which is always implicitly passed when you call it. I think it
translates to C++'s 'this' keyword, but I may be wrong. Simply put: The
first argument in an instance-method definition (be it called 'self' or
otherwise) refers to the current instance.
Note however that you don't explicitly pass the instance to the method,
that is done automatically:
j = Jeremy() # Jeremy.__init__ is called at this moment, btw
j.add_item("hi") # See? 'item' is the first arg you actually pass
I found this a bit confusing at first, too, but it's actually very
clean, I think.
/W
Thanks for all the replies, very impressive. Got it now.
Jeremy.
On Jul 12, 4:23 pm, Jeremy Lynch <jeremy.ly...@gmail.comwrote:
Hello,
Learning python from a c++ background. Very confused about this:
============
class jeremy:
list=[]
def additem(self):
self.list.append("hi")
return
temp = jeremy()
temp.additem()
temp.additem()
print temp.list
>
temp2 = jeremy()
print temp2.list
==============
The output gives:
['hi','hi']
['hi','hi']
Why does adding items to one instance produce items in a separate
instance? Doesn't each instance of jeremy have its' own "list"?
Many thanks for clearing up this newbie confusion.
Jeremy.
Alex Popescu a écrit :
(snip)
>
You are defining the list in the class context and so it becomes a
class field/member.
'attribute' is the pythonic term.
On Jul 13, 6:02 am, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
Alex Popescu a écrit :
(snip)
You are defining the list in the class context and so it becomes a
class field/member.
'attribute' is the pythonic term.
Thanks! I'm just a couple of weeks Python old, so I am still fighting
to use the correct vocabulary :-).
And as with foreign languages, while I am becoming better at reading I
still have problems expressing
correct ideas using correct words.
../alex
--
..w( the_mindstorm )p. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jeff Wagner |
last post by:
I've spent most of the day playing around with lists and tuples to get a really good grasp on what
you can do with them. I am still left with a question and that is, when should you choose a list or...
|
by: Kakarot |
last post by:
I'm gona be very honest here, I suck at programming, *especially* at
C++. It's funny because I actually like the idea of programming ...
normally what I like I'm atleast decent at. But C++ is a...
|
by: Chris Ritchey |
last post by:
Hmmm I might scare people away from this one just by the title, or
draw people in with a chalange :)
I'm writting this program in c++, however I'm using char* instead of
the string class, I am...
|
by: Jimmy Cerra |
last post by:
I recently came up with a cool little stylesheet for definition
lists. There is a small demostration for the impatient . I hope
this helps someone.
Here's how I did it. Definition lists are...
|
by: andrew |
last post by:
Hi,
I'm a C++ newbie, so apologies for this rather basic question. I've
searched for help and, while I understand the problem (that the outer
class is not yet defined), I don't understand what...
|
by: Claudio Grondi |
last post by:
Trying to understand the outcome of the recent
thread (called later reference thread):
"Speed quirk: redundant line gives six-fold speedup"
I have put following piece of Python code together:...
|
by: Paulo da Silva |
last post by:
Hi!
What is the best way to have something like the bisect_left
method on a list of lists being the comparision based on an
specified arbitrary i_th element of each list element?
Is there,...
|
by: Gordon Airporte |
last post by:
This is one of those nice, permissive Python features but I was
wondering how often people actually use lists holding several different
types of objects.
It looks like whenever I need to group...
|
by: colin |
last post by:
Hi,
I have 3 object of interest wich are objects in 3d space,
surface,wire,point.
they are all interconnected,
and they all contain lists of objects they are connected to,
eg each surface will...
|
by: twdesign |
last post by:
I am a Python newbie! I'm trying to list the feature classes in a dataset (Access .MDB) and then list the fields in each feature class.
I'm able to list the feature classes and I'm able to list...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| |