-1
def fun(m):
    i=0

    for _ in range(0,len(m)):
      m[_]=m[_]//2
   return m

list=[1,3,6] #User input long value or small value
x=fun(list[:])
print(x)

my question is: if the code become User input the list value the code's resule is can not sure?

because you can see Python: two objects are the same

>>> a = 4534534
>>> b = a
>>> a is b
True
>>> b = 4534534
>>> a is b
False
>>> a = 1
>>> b = a
>>> a is b
True
>>> b = 1
>>> a is b
True
Community
  • 1
  • 1
Chia's JaJa
  • 145
  • 1
  • 2
  • 5
  • @MartijnPieters I've been fighting this battle as well :) – RocketDonkey Nov 24 '12 at 05:54
  • There, that's your formatting fixed. Your question itself however, makes *no sense* whatsoever. – Martijn Pieters Nov 24 '12 at 05:56
  • @MartijnPieters Haha, this is some fun revision history :) – RocketDonkey Nov 24 '12 at 05:56
  • 2
    @Chia'sJaJa -- You shouldn't use `is` to check for equality. Python interns small integers, so it works for numbers -5 through 256. It stops working at -6 and at 257. In other words, to check for equality, you want to use `==`. I don't know if that addresses your question or not, but it explains the code in the second part ... – mgilson Nov 24 '12 at 06:00
  • my code x=f(list[:]) so m=[1,3,6] we can see list=[1,3,6] m=[1,3,6] so...if list=[9999999,3,6] m=[9999999,3,6] ????why can not different ????(he also can //2 , why????) – Chia's JaJa Nov 24 '12 at 06:23
  • 1
    As a side note, you can also use `def fun(m): return [x//2 for x in m] if you want to get rid of a few variables. – RocketDonkey Nov 24 '12 at 06:25
  • SO~My code does not have the correct results???? – Chia's JaJa Nov 24 '12 at 06:28
  • @RocketDonkey if you had put `return [x//2 for x in m]` as an answer, I would have up voted it. – jimhark Nov 24 '12 at 06:31
  • I think what we are trying to sort out is what your true intention is. If you actually want the 'outside' list to be modified by the function, you would have to do something like use `global` to reference the list outside of the function scope. Again though, not sure if that is what you're looking for. – RocketDonkey Nov 24 '12 at 06:32
  • @jimhark Ha, still not 100% clear what the objective is, so I don't know how helpful it would be. Thanks though :) – RocketDonkey Nov 24 '12 at 06:36
  • >>> a=[1,2,3] >>> b=[1,2,3] >>> print(a[0] is b[0]) True >>> a=[999999,2,3] >>> b=[999999,2,3] >>> print(a[0] is b[0]) False – Chia's JaJa Nov 24 '12 at 06:37
  • true and false , so my code x=f(list[:]) become list=[1,3,6] m=[1,3,6] – Chia's JaJa Nov 24 '12 at 06:39
  • @mgilson's answer actually addresses the issue you are referencing. Since you are using `is` to compare integers, comparisons of equal numbers between -5 and 256 will always be `True` since they are stored by Python by default. Since 999999 is outside of that range, two objects are created, and `is` returns `False` (though `==` would return `True`). – RocketDonkey Nov 24 '12 at 06:39
  • if list=[1,3,6] m=[1,3,6] #true – Chia's JaJa Nov 24 '12 at 06:40
  • but....my code answer always can //2 , why????? – Chia's JaJa Nov 24 '12 at 06:43
  • It is not an exac5 duplicate, but answer to your question lies [here](http://stackoverflow.com/q/306313/257972) `The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object.` [from python docs](http://docs.python.org/2/c-api/int.html#PyInt_FromLong) – Mp0int Nov 24 '12 at 09:48

2 Answers2

1

I'm not sure I'm reading your question correctly. Sorry for that. But your code shows something surprising in Python. This is fairly expected:

>>> a = 4534534
>>> b = 4534534
>>> a is b
False

Python created two separate objects. That's the way Python usually works.

>>> a = 1
>>> b = 1
>>> a is b
True

What? Well, as an optimization Python shares small integers from a pool so they will have the same id.

Is that what you were trying to ask about?

If you write:

m = list[:]

A shallow copy of list is made and assigned to m. The object id's of the lists will be different while the object ids of the contents will be the same. Your list contains ints, which are are immutable so the values in your original list can not be changed by reference to a copy of the list. However when a list contains mutable values (such as objects, dicts, or even other lists) then modification to a shared mutable item in the list will be reflected in the original list.

The // operator causes the result to be rounded down to the nearest integer. Type is maintained though, so if one of the arguments is float, the result will be float.

That depends on what your definition of is is

a[0] is b[0]

is returns True when the objects are the same, that means the id's (think pointers) are the same. False is returned when the objects are different. The values may or may not be the same when the id's are different. If you want to test if values are the same (which is much more common) use ==.

Good luck, I hope this helps.

jimhark
  • 4,938
  • 2
  • 27
  • 28
  • my code x=f(list[:]) so m=[1,3,6] we can see list=[1,3,6] m=[1,3,6] so...if list=[9999999,3,6] m=[9999999,3,6] ????why can not different ????(he also can //2 , why????) – Chia's JaJa Nov 24 '12 at 06:17
1

I think you're asking if it is guaranteed that list (your variable) won't be changed when you run fun(list[:]).

Since int is an immutable type in python, you're completely safe. The interning of small integers (range from -5 to 256) that Cpython uses doesn't change anything here -- even if you code used m[_] //= 2 it would still be OK since that implicitly creates a new reference to an integer and stores it in your copied list (not the original).

As a side note list is a bad name for a variable. You're shadowing the builtin list type.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • +1 for the answer and the decoding - this would be my best guess as to the OP's intention. – RocketDonkey Nov 24 '12 at 06:06
  • my code x=f(list[:]) so m=[1,3,6] we can see list=[1,3,6] m=[1,3,6] so...if list=[9999999,3,6] m=[9999999,3,6] ????why can not different ????(he also can //2 , why????) – Chia's JaJa Nov 24 '12 at 06:23
  • @Chia'sJaJa -- I'm sorry, I don't understand your question. Do you want `x` and `list` to be the same or do you want them to be different? – mgilson Nov 24 '12 at 06:25
  • I think my code does not determine the result of – Chia's JaJa Nov 24 '12 at 06:31
  • because >>> a=[1,2,3] >>> b=[1,2,3] >>> print(a[0] is b[0]) True >>> a=[999999,2,3] >>> b=[999999,2,3] >>> print(a[0] is b[0]) False – Chia's JaJa Nov 24 '12 at 06:34