-1

This is something that has been in my mind since I knew about .sort() and random.shuffle. although I couldn't find how .sort() works but I went through the Lib\random.py of course it was promising enough to be understood that how random.shuffle(list_name) shuffles the list_name itself but I found difficulty understanding how it worked there....

What I'm trying to understand:

>>> lst = [-5, 2, 0, -2, -4, -3, 3, 4, -1, 5, 1]
>>> lst.sort() #How this changes the value of list???
>>> lst
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
>>> random.shuffle(lst) #How does this function change the value of argument itself?
>>> lst
[-2, 2, -3, 1, 0, 5, -5, 4, 3, -1, -4]

A simple example will be helpful to understand:

>>> a = [1,3,5,7,9]
>>> a.square()
>>> a
[1,9,25,49,81]
>>> b = [-2,-1,0,3,4]
>>> cube(b)
>>> b
[-8,-1,0,27,64]

How can I make the above code happen?

def kyub(lst_):
    return [i**3 for i in lst_]

here the above kyub() returns a list what I am expecting is to return nothing but change the lst_ itself.

To sum up

Assume we have a function y = f(x) which means for some value of x=x0 there will be some y=f(x0) which is similar to function = lambda x:f(x) but I'm looking for a function g(x) that returns nothing(None) but changes the value of x itself.

Solution to the below problem can help me understand the logic behind this.

>>> x_list = [value1, v2, v3, v4]
>>> print(x_list)   
[value1, v2, v3, v4]
>>> function(x_list)    
>>> print(x_list)
[output_1, output_2, output_3, output_4] #Note: I printed the value of x_list which now has changed. it's not what I'd printed it before.
  • Why _wouldn't_ they be able to? The whole point of methods is that they allow you to interact with an object's internal state. You can't do it with e.g. integers because they're _immutable_. – jonrsharpe Jun 22 '22 at 19:08
  • I personally always found the `random` API pretty annoying in that regard. As far as `lst.sort()` that's just a standard mutation. I generally avoid mutation at all costs, personally. – ddejohn Jun 22 '22 at 19:11
  • Where are those `.square()` and `cube()` methods from? – ddejohn Jun 22 '22 at 19:13
  • Sorry, just noticed your last question. So you're trying to implement both of those functionalities? Why? Generally one should *avoid* designing an API like that. – ddejohn Jun 22 '22 at 19:17
  • @jonrsharpe Yes, the word `immuatable` is enough but let's try something different `lst = [1,2,3,4]` and now let us assume there is a method `.square()` which if `lst.square()` will change the `lst = [1,4, 6, 16]` if it's possible can we do that? – Nikola Markovic Jun 22 '22 at 19:25
  • Are you asking how to add methods to built-in types? Or maybe you want to implement your own Sequence with that method? – jonrsharpe Jun 22 '22 at 19:26
  • @ddejohn *Where are those .square() and cube() methods from?* by those last `.square() and `cube()` I meant, "Is it possible for us to write a method that changes the value of object itself or a function that can change the value of argument itself" – Nikola Markovic Jun 22 '22 at 19:27
  • @jonrsharpe Yes you got me correct I want to understand how can I write me own `class` with some methods like `.square()` that will change the value of object itself.... – Nikola Markovic Jun 22 '22 at 19:29
  • 2
    What's what _most_ methods do, so I guess start with https://docs.python.org/3/tutorial/classes.html. – jonrsharpe Jun 22 '22 at 19:30
  • @jonrsharpe thnx for the [python documentation](https://docs.python.org/3/tutorial/classes.html) but I have gone through this one also in fact I have this `pdf` downloaded. but my bad couldn't find any example where I can see if there anyway I can do what I'm expecting in the question – Nikola Markovic Jun 22 '22 at 19:35
  • Your examples in the question are not adding methods to your own classes, which is what you say you want to do, so this makes no sense. You can't (aside from hacks like https://pypi.org/project/forbiddenfruit/) add methods to the built-in types. – jonrsharpe Jun 22 '22 at 19:37
  • `a.square()` you can't, `cube(b)` is an easy function you should _try to write yourself_. – jonrsharpe Jun 22 '22 at 19:52
  • @jonrsharpe Yes, to return it is very easy what not easy is to get my argument updated >>>I updated my question.... – Nikola Markovic Jun 22 '22 at 19:57
  • You've written a function that returns a new list, _explicitly_. If that's not the behaviour you want, _write a different one_. SO is **not** a code-writing service, we're not here to replace basic tutorials and _practice_ - review basic list manipulation. – jonrsharpe Jun 22 '22 at 19:59
  • @jonrsharpe Yes your are right I understand that but I'm asking something different maybe I'm just not able explain it what I'm looking for which I'm sure that my question will not be going to `tutorials or practice` category... – Nikola Markovic Jun 22 '22 at 20:04
  • It seems like you're asking how to update the values in a list, which is very much in the tutorial category (or would be, if we had one; it's **definitely** in the tutorial I've already linked you to). If that's the not the case you'll need to edit substantially to explain what you're really asking. – jonrsharpe Jun 22 '22 at 20:06
  • @jonrsharpe I have been in coding infact I understood your [beautiful answer](https://stackoverflow.com/questions/31026754/python-if-not-vs-if/31026976#31026976) of interesting question. I mean: I'm not asking somethin basics I can understand complicated things. – Nikola Markovic Jun 22 '22 at 20:08
  • That's as may be, all we can go on is what's in the question. – jonrsharpe Jun 22 '22 at 20:11
  • @jonrsharpe I'll come up with more research and after going through the `Lib\modules.py` codes again till then we can keep this question here in the name of efforts done by you(plural/including others) in adding these valuable comments. – Nikola Markovic Jun 22 '22 at 20:18

1 Answers1

0

Lists are mutable, meaning you can change their length, content, etc without creating a new object. So you can say

lst = []
lst.append(1)

and list will be [1].

Integers are immutable. So if you say

a = 5
a = a + 1

you aren't taking the 5 object and adding one, you are taking the value of a (5), adding 1, creating a new object, and assigning a to that object (actually python caches integers so its not exactly that but for this example it's fine.

If you want the same functionality, you have to assign a the new value

a = a.square()

Basically, you can do this to lists because thats how python is written. There are some functions on lists that change the current object, and some that return a copy of the list or values from the list. You just have to read the documentation to see how it handles the list

Loydms
  • 156
  • 3
  • Okay thnx i got you, but can we write a method `.square()` which will work as `lst = [2,6,9]` `lst.square() >>> [4, 36, 81]` Now, is it possible to write a code for `.square()` method? – Nikola Markovic Jun 22 '22 at 19:31
  • Sure its possible. The reason you can use sort() on list, is because list is a python object with the class method of sort(). You can either write a function that takes a list as an input and changes its contents (or returns a new list, squared) or you can write a new class that inherits from list and adds a squire method. The most pythonic way to do it would be list = [i **2 for i in list] – Loydms Jun 22 '22 at 19:35
  • Can you please provide me the snipped code? but please I'm not expecting `function = lambda lst: [i**2 for i in lst]` which will give me `function([2,5,9]) >>> [4, 25, 81]` – Nikola Markovic Jun 22 '22 at 19:39
  • What are you expecting? You want list objects to have a built in method you can call via a period? i.e. you want list.square() NOT square(list)? – Loydms Jun 22 '22 at 19:44
  • @NikolaMarkovic if you just want to know how to assign a value into a list, that's covered by tutorials too: https://docs.python.org/3/tutorial/introduction.html#lists – jonrsharpe Jun 22 '22 at 19:48
  • Both will be okay but instead of `return` they should change the value of `list_` itself. like `random.shuffle(list_)` shuffles the `list_` instead of `returning` a shuffled `list_` – Nikola Markovic Jun 22 '22 at 19:51
  • 1
    Just write a function that loops through the indexes in your list and assigns each index the squared value of the current value – Loydms Jun 22 '22 at 19:55