Hi,
How do python instances work?
Why does the code at the end of my posting produce this output:
list in a:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list in b:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
instead of
list in a:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list in b:
[]
----------------------------
class MyClass:
list = []
def add(self, x):
self.list.append(x)
def printer(self):
print self.list
a = MyClass()
b = MyClass()
for n in range(10):
a.add(n)
print "list in a:"
a.printer()
print "list in b:"
b.printer()
/H 6 1383
Hi, class MyClass: list = []
you have "list" defined as a classmember, not an instancemember. So
"list" ist defined ONCE for all instances.
Try this instead:
class MyClass:
def __init__(self):
self.list = []
[...]
and use self.list ...
HtH, Roland
Guess i shouldn't think of the __init__(self) function as a constructor
then.
Thanks.
/H Guess i shouldn't think of the __init__(self) function as a constructor then.
__init__ is THE constructor in Python
--
__________________________________________________ _______________
Laszlo Nagy web: http://designasign.biz
IT Consultant mail: ga*****@geochemsource.com
Python forever!
On 20 Apr 2005 00:44:53 -0700, he***********@hotmail.com wrote: Guess i shouldn't think of the __init__(self) function as a constructor then. Thanks.
Depends on what you think when you think "constructor" ;-)
Read about both __new__ and __init__. The former is always
necessary to create an object, and __init__ may take parameters to
define intial state from its parameters, but __new__ does the whole
job for immutables. I.e., "constructor" translates to combination of
both if both are present, but __new__ must be always be there and
come first. In general there are default methods inherited from
object and/or type, the most primitive classes, so you don't have
to define them except to customize for your purposes.
At least, that's the way I think of it ;-)
Regards,
Bengt Richter he***********@hotmail.com wrote: Guess i shouldn't think of the __init__(self) function as a constructor then.
No, that's not it. You shouldn't think of variables defined outside of a method as instance variables.
In Java for example you can write something like
public class MyClass {
private List list = new ArrayList();
public void add(Object x) {
list.add(x);
}
}
In this case list is a member variable of MyClass instances; 'this' is implicit in Java.
In Python, if you write something that looks similar, the meaning is different:
class MyClass:
list = []
def add(self, x):
self.list.append(x)
In this case, list is an attribute of the class. The Java equivalent is a static attribute. In
Python, instance attributes have to be explicitly specified using 'self'. So instance attributes
have to be bound in an instance method (where 'self' is available):
class MyClass:
def __init__(self):
self.list = []
def add(self, x):
self.list.append(x)
Kent he***********@hotmail.com wrote: Hi,
How do python instances work? Why does the code at the end of my posting produce this output:
list in a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] list in b: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
instead of
list in a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] list in b: []
----------------------------
class MyClass: list = []
def add(self, x): self.list.append(x)
def printer(self): print self.list
a = MyClass() b = MyClass()
for n in range(10): a.add(n)
print "list in a:" a.printer() print "list in b:" b.printer()
/H
because list is a class member not an instance member (not sure about
the vocabulary) if in __init__ you set self.list=[] you'll get the
result you want! By declaring list=[] in the class it is shared between
all instances!
--
EuGeNe
[---- www.boardkulture.com www.actiphot.com www.xsbar.com
----] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: none |
last post by:
or is it just me?
I am having a problem with using a dictionary as an attribute of a
class. This happens in python 1.5.2 and 2.2.2 which I am accessing
through pythonwin builds 150 and 148...
|
by: Barry A. Warsaw |
last post by:
On behalf of the Python development team and the Python community, I'm
happy to announce the release of Python 2.3 (final).
Nineteen months in the making, Python 2.3 represents a commitment to...
|
by: Brandon J. Van Every |
last post by:
What's better about Ruby than Python? I'm sure there's something. What is
it?
This is not a troll. I'm language shopping and I want people's answers. I
don't know beans about Ruby or have...
|
by: David MacQuigg |
last post by:
Playing with Prothon today, I am fascinated by the idea of eliminating
classes in Python. I'm trying to figure out what fundamental benefit
there is to having classes. Is all this complexity...
|
by: David MacQuigg |
last post by:
I am starting a new thread so we can avoid some of the non-productive
argument following my earlier post "What is good about Prothon". At
Mr. Hahn's request, I will avoid using the name "Prothon"...
|
by: Rahul |
last post by:
Hi.
I am part of a group in my univ where we organize a programming
contest. In this contest we have a UDP based server. The server
simulates a game and each contestant is to develop a team of...
|
by: Ben Finney |
last post by:
Howdy all,
PEP 354: Enumerations in Python has been accepted as a draft PEP. The
current version can be viewed online:
<URL:http://www.python.org/peps/pep-0354.html>
Here is the...
|
by: John Nagle |
last post by:
The major complaint I have about Python is that the packages
which connect it to other software components all seem to have
serious problems. As long as you don't need to talk to anything
outside...
|
by: Marcin Kalicinski |
last post by:
How do I use multiple Python interpreters within the same process?
I know there's a function Py_NewInterpreter. However, how do I use functions
like Py_RunString etc. with it? They don't take any...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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,...
| |