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 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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....
|
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
|
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...
| |