Class Chaos

M

Maximilian Michel

Hallo everyone,

i hope someone can help me with this kind a interesting problem i have
in python 2.3:

my code looks like this:

class Read:
list = []
__init__(self):
self.list.append = ***data from file***
print(self):
print self.list

instantiating it one time works ok, assuming data from file = AAA:
...
a = Read()
AAA
but when i continue, instantiating more object of the Read class this
happens:
b = Read()
AAA
AAA
c = Read()
AAA
AAA
AAA
it's like the new instance always continues or reimports the data from
the former instance.
Or is it just a dumb mistake i keep making?


Thanks for your help,
max
 
Y

Yermat

Maximilian said:
Hallo everyone,

i hope someone can help me with this kind a interesting problem i have
in python 2.3:

my code looks like this:

class Read:
list = []
__init__(self):
self.list.append = ***data from file***
print(self):
print self.list

instantiating it one time works ok, assuming data from file = AAA:
...
a = Read()
AAA
but when i continue, instantiating more object of the Read class this
happens:
b = Read()
AAA
AAA
c = Read()
AAA
AAA
AAA
it's like the new instance always continues or reimports the data from
the former instance.
Or is it just a dumb mistake i keep making?

Of course !
list is a class attribute so it is share by all instances !
You should initialize list in the __init__ method :

class Read:
def __init__(self):
self.list = []
self.list.append...
 
H

Helmut Jarausch

Maximilian said:
Hallo everyone,

i hope someone can help me with this kind a interesting problem i have
in python 2.3:

my code looks like this:

class Read:
list = []
__init__(self):
self.list.append = ***data from file***
print(self):
print self.list

instantiating it one time works ok, assuming data from file = AAA:
...
a = Read()
AAA
but when i continue, instantiating more object of the Read class this
happens:
b = Read()
AAA
AAA
c = Read()
AAA
AAA
AAA
it's like the new instance always continues or reimports the data from
the former instance.

The assignment list=[] is made only once when the class definition
gets compiled. So your __init__ method uses one and the same list object
all the time.
When you create a new object of class 'Read' only the __init__ method gets
executed.
If you shift the 'list=[]' statement within the __init__ method it will
work as you probably like it to.

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
P

Paul McGuire

Maximilian Michel said:
Hallo everyone,

i hope someone can help me with this kind a interesting problem i have
in python 2.3:

my code looks like this:

class Read:
list = []
__init__(self):
self.list.append = ***data from file***
print(self):
print self.list
<snip>

You will also find that you avoid some funny Python behavior if you avoid
using built-in type names (such as list, dict, and str) for variable names.

-- Paul
 
L

Leif K-Brooks

Paul said:
You will also find that you avoid some funny Python behavior if you avoid
using built-in type names (such as list, dict, and str) for variable names.

What sort of funny behavior?
 
T

Tristan Seligmann

What sort of funny behavior?

Code that uses the names without expecting them to be masked by other
objects:

str = "Blah blah"
.... later ...
str(5)

Produces the slightly mysterious:
TypeError: 'str' object is not callable

Correct behavior, to be sure, but potentially confusing.
--
mithrandi, i Ainil en-Balandor, a faer Ambar

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA4Fx6pNuXDQIV94oRAll1AJ45ZWeeJHFCTgSgTjJUyGGui6Tg7wCfZ05S
l2jdkRJ3W4oc5gxB3MlNlT4=
=keHq
-----END PGP SIGNATURE-----
 
P

Paul McGuire

Leif K-Brooks said:
names.

What sort of funny behavior?

Unexpected masking of the built-in classes, their static methods and
constructors. F'rinstance:
str.join("",["A","B","C"]) 'ABC'
str = "X"
str.join("",["A","B","C"])
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: join() takes exactly one argument (2 given)


Here's a constructor example:
list() []
list = [ 1,2,3,4]
list()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'list' object is not callable


Here's a more insidious constructor collision example (admittedly contrived,
but note, this one doesn't raise an exception!):
list() []
list = (lambda a=10: [ i for i in range(a)] )
list()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


It is too bad that these class names are *so* natural and intuitive, that
one just *wants* to name some temporary list as 'list' or dict as 'dict'.

-- Paul
 
R

Reinhold Birkenfeld

Paul said:
It is too bad that these class names are *so* natural and intuitive, that
one just *wants* to name some temporary list as 'list' or dict as 'dict'.

Doesn't PyChecker complaint about such variable names being used?

Reinhold
 
C

Christopher Baus

Hi,

I'm just learning python after years of C/C++/Java/bash/etc. It is going
pretty good except a few minor issues regarding syntax. I have to admit
the while/else contruct is a bit weird, but I think I understand the
motivation. But I am confused about exceptions having read the chapter in
Learning Python.

How do I do something similar to the following java code?

try{
a.mightThrow();
}
catch(Exception e){
System.out.print("looks like an exception");
}
finally{
a.cleanup();
}

try:
pass
finally:
pass

Doesn't allow the programmer to catch certain exceptions, handle them, and
then perform operations in either case.
 
R

Reinhold Birkenfeld

Christopher said:
Hi,

I'm just learning python after years of C/C++/Java/bash/etc. It is going
pretty good except a few minor issues regarding syntax. I have to admit
the while/else contruct is a bit weird, but I think I understand the
motivation. But I am confused about exceptions having read the chapter in
Learning Python.

How do I do something similar to the following java code?

try{
a.mightThrow();
}
catch(Exception e){
System.out.print("looks like an exception");
}
finally{
a.cleanup();
}

try:
pass
finally:
pass

Doesn't allow the programmer to catch certain exceptions, handle them, and
then perform operations in either case.

This is, unfortunately, a FAQ:

You must use

try:
try:
<statements>
except WhatEver:
<statements>
finally:
<statements>

This is due to the fact that try-except and try-finally are two
different constructs; there was a thread here not long ago, starting
with Message-ID <[email protected]>
(if you use a news server to access comp.lang.python).

Reinhold
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top