Python Forum
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python pointers?
#1
I haven't used Python in forever so forgive me for my lack of knowledge.

However recently I have been learning about C/C++ and I was wondering.. How do you implement pointers in Python?

Pointers are essential for creating data structures in C/C++ but what about Python? Python has its own library of structures like dictionaries and you name it.. but what if I wanted to make my own data structure?

How would you:
-> Implement linked lists in Python on your own using pointers
Reply
#2
Not many high-level languages have pointers due to security reasons. But languages themselves do implement the datatypes and do memory management using pointers.

In Python, the functionality of linked list is achievable by other native data types like lists. There is collections.deque that is implemented as doubly linked list.

However, if you want to implement a linked list yourself you can use classes to do so. Example.
Reply
#3
I would say that python has only pointers in the sense that every variable is a pointer to a python object. For exemple
>>> b = [3, 4]
>>> c = b
>>> c[1] = 8
>>> b
[3, 8]
The c = b assignment works exactly like pointers assignment in C.

What we don't have in python is hard references and pointers to pointer. If we want to reference a reference, we do it symbolically, for example
>>> varname = 'c'
>>> globals()[varname] = 'spam'
>>> c
'spam'
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020