Main Content
Class, static and unbound variables Archive - Originally posted on "The Horse's Mouth" - 2005-10-25 14:47:07 - Graham Ellis
In Object Oriented programming, you'll have certain named blocks of code (usually known as methods) that you can perform on specific objects (type A), and others that you can perform on all the objects of a particular type (type B).
Example.
If I had a class called sandwich, I might have one method called getFilling and another called getBread which return me information as to whether a particular sandwich is Beef or Cheese, and whether it's on brown or white bread. These methods are both Type A as their actions will vary depending on which sandwich they're run.
However, if I have a method called getCount that returns me a total of the number of sandwiches I have, then it's going to return me the same value whether I call it on my "Beef and Stilton on White", or on a Plain cheese sandwich in brown bread. So that's an example of a Type B.
Type B methods are sometimes known as Class methods since they apply to the class as a whole. You'll also find that Java programmers refer to them as static methods, and Python fans call them unbound methods since they're not attached (bound) to any particular single instance of an object.
Type A methods, then, may be known as Object methods as they apply to an individual object, dynamic methods as they change in behaviour depending on which particular object they're called on, or bound methods because their behaviour is bound to a certain object.
Some other articles
Y112 - Objects - Intermediate Nesting decorators Defining an object that is a modified standard type in Python with in Python - examples of use, and of defining your own context Object and Static methods - what is the difference; example in Python 3 Setting up and tearing down with the Python with keyword Deciding whether to use parameters, conditional statements or subclasses Spike solution, refactoring into encapsulated object methods - good design practise A good example of recursion - a real use in Python Changing what operators do on objects - a comparison across different programming languages Object factories in C++, Python, PHP and Perl Python base and inherited classes, test harness and unit testing - new examples Python Properties - how and why Really Simple Class and Inheritance example in Python Inheritance, Composition and Associated objects - when to use which - Python example Backquote, backtic, str and repr in Python - conversion object to string Metaclasses (Python) and Metatables (Lua) Static variables in functions - and better ways using objects A demonstration of how many Python facilities work together A list of special method and attribute names in Python Python - some common questions answered in code examples Defining static methods in Python Should Python classes each be in their own file? The Light bulb moment when people see how Object Orientation works in real use Python decorators - your own, staticmethod and classmethod Mixins example in Python Multiple inheritance in Python - complete working example The Multiple Inheritance Conundrum, interfaces and mixins Methods that run on classes (static methods) in Python How do I set up a constant in Python? TypeError: super() argument 1 must be type, not classobj (Python) Python - fresh examples of all the fundamentals Calling base class constructors Equality, sameness and identity - Python Using a utility method to construct objects of different types - Python Python - formatting objects What are factory and singleton classes? __new__ v __init__ - python constructor alternatives? Practical polymorphism in action Pieces of Python Comparison of Object Oriented Philosophy - Python, Java, C++, Perl Think about your design even if you don't use full UML This article Overloading of operators on standard objects in Python Using a Python dictionary as a holder of object attributes Y106 - Object Oriented Python When to check an object type - Python isinstance example Why populate object with values as you construct them? From single block to structure and object oriented programming What is the difference between a function and a method? Simple OO demonstration in C++, comparison to Python Really Simple Class and Inheritance example in Python Spike solution, refactored and reusable, Python - Example this or self - what are they, and what is the difference? (Python) From Structured to Object Oriented Programming. Object oriented or structured - a comparison in Python. Also writing clean regular expressions Moving from scripting to Object Orientation in Python From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) Object Oriented Programming for Structured Programmers - conversion training Tips for writing a test program (Ruby / Python / Java) When should I use OO techniques? Python - a truly dynamic language Introduction to Object Oriented Programming Screw it or Glue it? Access to Object variables - a warning Python class rattling around Python - function v method Python makes University Challenge This article Q909 - Object Orientation: Composite Objects Deciding whether to use parameters, conditional statements or subclasses Philosophy behind object design - and how I applied in to a Java example Designing a base class and subclasses, and their extension, in C++ Extended and Associated objects - what is the difference - C++ example How do classes relate to each other? Associated Classes C++ - objects that are based on other objects, saving coding and adding robustness Jargon busting Private and Public - and things between Getting the OO design write - with PHP a example Relationships between Java classes - inheritance, packaging and others Object Oriented Programming in PHP Designing a heirarcy of classes - getting inheritance right Screw it or Glue it? Access to Object variables - a warning Perl and Shell coding standards / costs of an IT project NOT Gone phishing This article J706 - Objects and Classes Objects - from physical to virtual or abstract - Java Binomial Coefficient (Pascal Triangle) objects in Java Calculation within objects - early, last minute, or cached? Defining a static method - Java, Python and Ruby Looking inside Java classes - javap and javadoc When should I use OO techniques? Introduction to Object Oriented Programming Long, Longer, Longest in Java First Class Java. First step and moving forward. An example of Java Inheritance from scratch A better alternative to cutting and pasting code This article Variable Scope