What does * and ** means in Python? Is it to do with pointers …

  • Answer to: “What is the pointer in Python”

    Background: I come from assembly language and games coding in C++, so I know what pointers are all about.

    It’s important to understand variables in Python are effectively pointers in practical use. The equal “=” assignment operator automatically creates an effective pointer – unless it is a basic type (called an immutable): int, float, str, bool are basic types, and these are not pointers of any description.

    lists, dicts, class objects etc are effectively pointers.

    Let’s stop talking, and SHOW you what it is all about with real-world examples.

    so if you have a variable “a” representing a list (which is not a basic type) :-

    ———

    a=[1,2,3]

    creates the list object and also creates (effectively) a pointer to it called “a”

    ———

    so if you assign :-

    b=a

    both a and b are pointers to the same list object

    so if you say :-

    a[0]=10

    both a and b will be :-

    [10,2,3]

    you can check they point to the same object with the “is” operator :-

    a is b

    will return True

    this is not the same as “==”

    which compares two different objects and gives True as long as the contents are the same

    ———————

    only when all pointers to the object are removed does python delete it.

    so you could

    del a

    or

    del b

    and python will still store the object

    only when you del the last pointer to it, is the object removed from memory.

    ———————

    remember I said simple types are not pointers

    so :-

    a=10

    b=a

    these are not pointing to the same object, as it is a simple type (int)*.

    a=100

    will not set b to 100, b will remain at 10

    PS. This is better than just using indexing as “pointers” which you can do in any language. As you can access all the members and sub members of an object you point to in the same way. So it acts as a proper pointer in C++. And it’s not a copy that needs to be updated after – as it shares the same memory. What’s not to like.


    Re: Krzysztof’s answer and comments on this answer.

    None of us is right or wrong here, despite what he initially said in the comments below. I feel my explanation is easier for beginners to “get it” quickly. The main thing is you understand what is going on, not the technical terms used, or to present too many outside details, to begin with, you will just get confused. Both answers complement each other, read mine first to “get” the key points quickly.

    Reminds me of this, one of my most favourite quotes of all time. “You can know the name of a bird in all the languages of the world, but when you’re finished, you’ll know absolutely nothing whatever about the bird… So let’s look at the bird and see what it’s doing — that’s what counts. Behaviour counts more than anything and I learned very early the difference between knowing the name of something and knowing something.”

    Richard Feynman

    US educator & physicist (1918 – 1988)

    And yes, to all practical purposes they behave effectively as pointers.

    Which is why I led this answer on real-world examples you will encounter 99% of the time – not getting too hooked up on the correct terminology used. Also, the “immutable” terminology as explained in computer science – breaks down in Python when using classes that have mixes of simple and complex data types – as the more complex data types are always pointers – you can always change them. Not actually “immutable”. Here, my explanation works better in predicting what is going on. (As it is closer to what is actually happening under the hood).

    A comment on the notion that “all data in python is an object” which was also mentioned in the comments section. Now follows a more long-winded explanation on that. No need to read if you just here to understand how pointers/ references work in python.

    It is not technically correct that everything in python is an object. This is something you are taught in computer science classes. But if we go deeper, int in python is not a true “object” in any normal sense. Yes, it appears to have methods if you do introspection (e.g. a dir) on it. However, it’s not really stored that way under the hood (would be crazy on memory). For example, you cannot add new member variables as you would in a normal object. All the int “object” really is – under the hood – is some flags which allow you to override certain operations. It’s good, it’s pythonic, it maintains the same consistent interface by making it look like an object. I’ve seen the python source for this and can assure you it’s not a real object by any means. You cannot add any kind of new attribute, method or variable. But you can override certain operations using the same interface as if it were an object, which is cool and pythonic. Why remember another syntax? I love Python as a language for that very reason, and it’s why it’s my fav. To me, this is elegant simplicity/beauty.

    The key advantage of python is it gets you going very quickly. It looks like pseudo-code and is very readable. This answer (in my opinion) fits with this philosophy of python. As you get more advanced and use it more – python is still very powerful – and there is all kind of details and tricks you can learn to get the very best from it and get it to do exactly what you require! Yes! You can have the power of C and be elegant.

    As discussed in the comments regarding this example, these technically have the same ID when you check. But in all practical terms of how it is used and functions, it all fits with the model described here. (As of me typing this, June 2020, that is the best argument anyone has made in the comments, and it did not matter in any practical terms).

Buy CBD Oil Arizona