1

Printing type of one variable just returns the pointed data's type

i = [5,6,7,8]
j = i
print(type(j))
<class 'list'>

and j references a mutable type. So

j[0] = 3
print(i)
print(j)
[3, 6, 7, 8]
[3, 6, 7, 8]

I want a function that returns true for j and false for i. If it's built-in or anyone could write that it would be appreciated.

6
  • 2
    is_pointer = lambda variable: False Commented Nov 22, 2022 at 4:37
  • 1
    @Samathingamajig well stated Commented Nov 22, 2022 at 4:37
  • See the answer below, you could i.__hash__ is None vs j.__hash__ is None, since immutable types are typically hashable, and mutable types typically are not (although there's no real guarantee - you can make either statement false if you really want to, but for practical cases this works) Commented Nov 22, 2022 at 4:54
  • "true for j and false for i." these are the same object, you can't even in principle write a function that could tell you what you are asking and it is the wrong question anyway Commented Nov 22, 2022 at 4:58
  • 2
    Variables don't reference variables. They reference objects. When you wrote j = i, python fully resolved i to an anonymous object and then assigned that object to j. j has no idea that the object happened to come from i. Commented Nov 22, 2022 at 5:09

1 Answer 1

2

There's no 'pointers' in Python, like there are in C++ (or similar languages). The only distinction in Python is mutable vs. immutable. But all variables are just references to objects.

Variables with immutable types refer to values that you cannot modify, only replace. int is an example of an immutable type.

When you run your code, you assign 5 to i, then you assign the value of i to j, so that j now also has the value 5, but since these are immutable values, you can only change the entire value of either variable, which won't affect the value of the other.

Variables with mutable types have values that you can modify. list is an example of a mutable type.

When you run this:

xs = [1, 2, 3]
ys = xs
xs[0] = 999

The last statement modifies the value of both variables, as the list that was the value of xs is the same list that was assigned to ys and the final instruction modifies that value.

Immutable types including numbers, strings and tuples as well as a few other simple types. Mutable types include lists, dictionaries, and most other more complex classes.

Also, for example have a look at this:

a = 1
b = 1
c = b + 1
d = 2
print(id(a), id(b), id(c), id(d))

This will print four numbers, but note how the first two numbers will be the same (as they both refer to 1) and the second two are the same as well (as they both refer to 2).

Having said all that, if you want to test if something is mutable, there's no easy way to do that - but a common reason to want to do so is because you want to test if something is hashable, which you could test:

s = 'test'
print(s.__hash__ is None)  # False


xs = [1, 2, 3]
print(xs.__hash__ is None)  # True
Sign up to request clarification or add additional context in comments.

1 Comment

Your example with id's really cleared things up for me. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.