Python Forum
Basic Pyhton for Rhino 6 question about variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Pyhton for Rhino 6 question about variables
#1
Hi all,

I've been trying to create a sequence in which a new value is based on the older value.
then the values change making the new value the old one and create a new value and so on in a for loop.

the problem I'm facing is when assigning a variable to a variable. If a change happens to one variable the other one changes to match the first one

here is a simple and easy example:

pt1 = [1,1,1]
pt2 = pt1
pt1[2] = pt1[2] * 2
print(pt1)
print(pt2)

the result that shows up is as follows:

[1, 1, 2]
[1, 1, 2]

isn't it supposed to be?

[1, 1, 2]
[1, 1, 1]


why did pt2 change after modifying pt1?
is there a way to assign the values of the variable instead of its path?

Thanks to anyone
Reply
#2
pt2 is a pointer to pt1, so they both share the same variable space. (an other name for the same variable)
id one changes since the other does as well since they are really one.
you can use the copy function if you want one to be a copy of and independent of the other.
https://docs.python.org/3/library/copy.h...odule-copy
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic Coding Question: Exit Program Command? RockBlok 3 504 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  Vlookup function in Pyhton antozas 1 590 Oct-02-2023, 04:16 AM
Last Post: vikraman
  [solved] Basic question on list matchiing paul18fr 7 1,809 May-02-2022, 01:03 PM
Last Post: DeaD_EyE
  Very basic calculator question BoudewijnFunke 4 1,884 Dec-10-2021, 10:39 AM
Last Post: BoudewijnFunke
  Reading Multiple text Files in pyhton Fatim 1 1,886 Jun-25-2021, 01:37 PM
Last Post: deanhystad
  basic question isinstance tames 5 2,773 Nov-23-2020, 07:20 AM
Last Post: tames
  basic question about tuples and immutability sudonym3 6 2,828 Oct-18-2020, 05:11 PM
Last Post: sudonym3
  Pyhton code help from head first with python Shaikat_99 3 3,102 Jun-07-2020, 09:12 AM
Last Post: snippsat
  Question regarding local and global variables donmerch 12 5,022 Apr-12-2020, 03:58 PM
Last Post: TomToad
  start interactive pyhton shell with pre-loaded custom modules Viktor 2 2,145 Apr-09-2020, 08:25 AM
Last Post: Viktor

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020