Linux Today: Linux News On Internet Time.

More on LinuxToday

developerWorks: Metaclass Programming in Python

Apr 11, 2003, 04:00 (2 Talkback[s])
(Other stories by David Mertz, Michele Simionato)


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

"Let's start with a 30-second review of just what OOP is. In an object-oriented programming language, you can define classes, whose purpose is to bundle together related data and behaviors. These classes can inherit some or all of their qualities from their parents, but they can also define attributes (data) or methods (behaviors) of their own. At the end of the process, classes generally act as templates for the creation of instances (at times also called simply objects). Different instances of the same class will typically have different data, but it will come in the same shape--for example, the Employee objects bob and jane both have a .salary and a .room_number, but not the same room and salary as each other.

"Some OOP languages, including Python, allow for objects to be introspective (also called reflective). That is, an introspective object is able to describe itself: What class does the instance belong to? What ancestors does that class have? What methods and attributes are available to the object? Introspection lets a function or method that handles objects make decisions based on what kind of object it is passed. Even without introspection, functions frequently branch based on instance data--for example, the route to jane.room_number differs from that to bob.room_number because they are in different rooms. With introspection, you can also safely calculate the bonus jane gets, while skipping the calculation for bob, for example, because jane has a .profit_share attribute, or because bob is an instance of the subclass Hourly(Employee)..."

Complete Story

Related Stories: