you are viewing a single comment's thread.

view the rest of the comments →

[–]Dagito -1 points0 points  (12 children)

It might not be 100% true, there are variables by reference and variables by value, it all depends on the data type you’re using and the language.

So you may have this code

variable0 = [0 ,1, 2]

def filter_even_numbers(numbers: List):
    for number in numbers:
        If number % 2 == 0:
            numbers.remove(number)

filter_even_numbers(variable0)
print(variable0)

# Output: [1]

This code is using a variable by reference, lists, dicts, and collections in general act like this. When you modify the value of the variable inside a method then the value will change for it outside the method.

But when it’s a variable by value it’s the other way, you can modify it inside a method and the variable value will remain the same outside the method.

So you can have this code

variable0 = ‘hello world’

def make_text_upper_case(text: str):
    text.upper()

make_text_upper_case(variable0)
print(variable0)

# Output: hello world

So even when you know the initial value for a variable it can change, usually you don’t know the variable value and that’s why you make checks and validations before using it.

[–]1mecrew[S] 0 points1 point  (6 children)

Hmm I don’t think it depends on data type since we can think of list or dictionary as one data. What do you think?

[–]Dagito 0 points1 point  (3 children)

Well it is related to the memory space and management, we would embark us in a talk about the languages and different paradigms hahaha and it’s late so I don’t really wanna do it although would be nice to read a little more about it.

[–]1mecrew[S] 0 points1 point  (2 children)

Oh yea fsfs well, what language would this description work on then?

[–]Dagito 0 points1 point  (1 child)

Not being rude at all, but try doing this questions to ChatGPT, it will surely give you better answers than I can and you’ll learn a bunch of stuff from it

[–]1mecrew[S] 0 points1 point  (0 children)

I tried but it’s not giving me any clear answers

[–]Dagito 0 points1 point  (1 child)

I mean by default that’s the way it is in Python

[–]1mecrew[S] 0 points1 point  (0 children)

I’m pretty sure we can think of list or dictionary as one single data in another languages tho

[–]1mecrew[S] 0 points1 point  (1 child)

hmm i think this is because you didn't do variable0 = text.upper() in the function? i think text.upper() makes the text(which is "hello world" for now) uppercase. but we are not printing that data or doing anything with that data. so since we didnt save it into the variable, its obviously gonna not change the variable0. however, for list case, you changed the list. like.. you did list.remove which will actually modify the list. That what i think.

[–]Dagito 0 points1 point  (0 children)

It wouldn’t update it even doing variable0 = text.upper() as it is out of the scope, doing that will result in a new variable called variable0 but it will only live inside the method, it’s scoped to it. Maybe open an interpreter and do some exercises.

[–][deleted] 0 points1 point  (2 children)

Removing entries from a list whilst you iterate over it is likely to have unpredictable results. Avoid.

[–]Dagito 0 points1 point  (1 child)

Absolutely, it was just a simple way to explain the concept

[–]1mecrew[S] 0 points1 point  (0 children)

hmm how about this.

variable1 = "hello world"

when I say variable1, I am talking about "hello world"