Simulated Pointers With Classes
In this lesson, you’ll simulate a pointer in Python using object-oriented programming principles. You’ll create a class to track metrics for an application. You’ll use the
@property decorator to access two counter values as if they were attributes.
In this video, I’ll show you how you can take advantage of object-oriented principles to mimic pointer behavior. What we want to do is create a
Metrics class that will contain two counters: one for counting the number of function calls and another for counting the number of cat pictures served.
I’ll use the
@property decorator so we can access this data as if it were an attribute. Now we just need a way to increment the counters. I’ll create two methods to do that, hardcoding in a
+= 1 so that neither counter can ever increase by more than 1 at a time.
01:52 One of the biggest benefits of pointers is reduced memory usage or, at least, manual control over the memory you’re using. Python doesn’t let us do that. Instead, we have to rely on the mutability of object types just to even mimic pointer behavior. Python is a high-level scripting language.
02:14 If the code you’re writing requires pointer-level memory management, then Python might not be the right choice of language, at least not for that part of your codebase. In the next and final lesson, I’ll show you where Python really shines: when you can interface it with C.
Become a Member to join the conversation.