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

Problem with class variables

Dag
I have a problem which is probaly very simple to solve, but I can't
find a nice solution.
I have the following code

class Test:
def __init__(self):
self.a=[1,2,3,4]
self.b=self.a
def swap(self):
self.a[0],self.a[3]=self.a[3],self.a[0]
def prnt(self):
print self.a
print self.b
c=Test()
c.swap()
c.prnt()

which returns
[4,2,3,1]
[4,2,3,1]

Now I understand why it is doing this, but it's not what I want. How to
I get it to return
[4,2,3,1]
[1,2,3,4]

Dag
Mar 29 '07 #1
3 945
In <sl********************@remote1.student.chalmers.s e>, Dag wrote:
I have a problem which is probaly very simple to solve, but I can't
find a nice solution.
I have the following code

class Test:
def __init__(self):
self.a=[1,2,3,4]
self.b=self.a
self.b = list(self.a)

BTW this is about instance variable, not class variables.

Ciao,
Marc 'BlackJack' Rintsch
Mar 29 '07 #2
On Mar 29, 8:43 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
In <slrnf0nfph.8f9.f98d...@remote1.student.chalmers.s e>, Dag wrote:
I have a problem which is probaly very simple to solve, but I can't
find a nice solution.
I have the following code
class Test:
def __init__(self):
self.a=[1,2,3,4]
self.b=self.a

self.b = list(self.a)

BTW this is about instance variable, not class variables.

Ciao,
Marc 'BlackJack' Rintsch
One of the many ways to do this:

class Test:
def __init__(self):
self.a=[1,2,3,4]
#self.b=self.a
def swap(self):
self.a[0],self.a[3]=self.a[3],self.a[0]
return self.a
def prnt(self):
print self.a
x = self.swap()
print x

c=Test()
c.prnt()
Mike

Mar 29 '07 #3
Dag schrieb:
I have a problem which is probaly very simple to solve, but I can't
find a nice solution.
I have the following code

class Test:
def __init__(self):
self.a=[1,2,3,4]
self.b=self.a
def swap(self):
self.a[0],self.a[3]=self.a[3],self.a[0]
def prnt(self):
print self.a
print self.b
c=Test()
c.swap()
c.prnt()

which returns
[4,2,3,1]
[4,2,3,1]

Now I understand why it is doing this, but it's not what I want. How to
I get it to return
[4,2,3,1]
[1,2,3,4]

Dag
Use a copy of self.a:
self.b = self.a[:]

Thomas
Mar 29 '07 #4

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

Similar topics

4
by: Martin | last post by:
I have some classes with statics public variables.... the problem is that the gcc compiles fine the files but in the linking process gcc says "multiple definitions of MyClass::Variable" or...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
1
by: Reinier Beeckman | last post by:
Hi, In a program i'm working on i got several classes. 3 of them have relation to my problem. Let's name them classes class A, class B and class C. classA { public static ClassA A = new...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
15
by: tatsudoshi | last post by:
Hello, I have this class http://pastebin.com/807571, where I set some variables on __construct. Originaly I set the $total_? variables when the function showLayout() was called. I know pastebin...
9
by: Rory Campbell-Lange | last post by:
We have a set of classes using static methods to retain reference variables between operations. The problem is that the static variables are not reset between operations when used through...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
5
by: tshad | last post by:
In VS 2003, I am setting up an abstract class that is setting up classes for each datatype of VB.Net (as well as C#). I am trying to set it up so that most of the work is done in the Abstract...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
5
by: YouPoP | last post by:
I have an MFC application (VS2005) with splitter window (one is the view other is a formview). you can select from the main menu different math operation on polynoms and polynoms shows on the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.