Affecting input vs producing output

I have always wondered about this so I thought I would ask the forum.

1- How do you know when a command changes the input as opposed to producing an output? I usually expect an output:

#temp1: [5,2,3,4,1] unchanged
#temp2: [1,2,3,4,5] produced sorted version of temp1

But then there are situations like this:

import random
#temp1: [4,3,2,1,5] shuffled
#temp2= Null, no output from shuffle

2- I feel like I’ve been in a situation where if you do temp2=temp1 and then proceed to change temp2, temp1 is also affected (this usually seems to happen with rhinocommon geometry- I script with GhPython). Why does this happen? I think I fixed it by doing something like temp2=temp1.duplicate() , assuming that temp1 is a rhino geometry. Is it good practice to always duplicate, even if it is a list of numbers?

  1. The documentation for those functions will tell you what will happen. sorted() generates according docs a new list, whereas the list.sort() sorts in place. Same for random.shuffle(), which will do its thing in place.

    temp1.sort() # sort temp in place

  2. temp2=temp1 doesn’t do a full copy. This is how Python works. You indeed need to make a full copy by duplicating the list.


You’ll need to read the python documentation to see which methods change things in place and which methods create copies. Remember also when you code a=b that you are simply setting a as a reference to b, you are not setting a to the current value of b. So, if you change b, a will change, because it references b. If you need to have independent copies of something there are many ways available, depending on what that something is. For example the easy way to copy a list is:


list_b will be a copy of the values of list_a at the moment the operation is performed, but no longer referenced to it, so if list_a changes list_b won’t.

There are also things like mylist.sort() which sorts mylist in place, and new_list=sorted(mylist) which creates a sorted copy. There is also the copy module to create copies of more complex data structures.


Is list_a=list_b the only situation where the contents are not copied, but referenced? I guess I have just been really lucky in past scripts that I have not generally made redundant lists. I tested it out, but have found that I can’t always get the referencing to work.

for i in range(len(list_b)):
#list_b: [2,3,4,5,6]
#list_a: [2,3,4,5,6]
#mynumber: 1 , did not change

Can you force the reference of individual items?

A little off topic but I was reading this thread and it occurred to me that I may have been using incorrect terminology.
What are objects?
What is the official name for items in a list?
What are items not in a list called? In the case of mynumber=10 and mystring=“abc” , what is mynumber and mystring called? And is 10 and “abc” their value?


Instances of classes and types


[quote=“lawrenceyy, post:4, topic:31388”]What are items not in a list called? In the case of mynumber=10 and mystring=“abc” , what is mynumber and mystring called? And is 10 and “abc” their value?
mynumber and mystring are variables. But so are like list_b and list_a. 10 and “abc” are indeed their values.

I can recommend as a good read on what Python is and how it works :slight_smile:



While we are down this path, when I type Rhino.Geometry.Point3d. a little window comes up with a bunch of options. Are the purple boxes called methods and the hand symbol called attributes? Ive noticed that you can’t put a () after the ones with the hand symbol.

Is shuffle() then a method of random?

hand symbols are indeed the properties, see it as variables attached to an object.
for example let’s say you create a point:
then point.X gives you it’s x value

the purple boxes are the methods, see these as functions related/attached to an object

there are also little list icons, which indicate there is an example available.