[Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Dec 25 23:56:44 CET 2012


On Tue, 25 Dec 2012 12:16:16 -0800, Rick Johnson wrote:

> On Tuesday, December 25, 2012 11:10:49 AM UTC-6, Dave Angel wrote:
> 
>> We all make mistakes, like my referring to class methods when I meant
>> instance methods.
> 
> This mistake reminded of how people in this group (maybe not you in
> particular) happily accept the terms "instance method" and "class
> method" HOWEVER if you _dare_ use the terms "instance variable" or
> "class variable" you get a swift rebuke.
> 
> Would anyone care for another serving of logically inconsistent py?

Rick, what makes you think that this is logically inconsistent?

"Method" is the accepted name for functions attached to classes. They 
report themselves as "methods":

py> class Test(object):
...     def spam(self):
...             pass
...
py> Test().spam
<bound method Test.spam of <__main__.Test object at 0xb7bf528c>>

There are two built-ins for creating different types of method: the 
classmethod and staticmethod functions. If you don't think that these 
objects should be called "<adjective> method", then what do you think 
that they should be called?

On the other hand, the terms "instance variable" and "class variable" are 
logically inconsistent with other terminology. It is common practice, 
across dozens or hundreds of languages, to refer to variables by their 
type (either as declared, in static-typed languages like C or Haskell, or 
as they are expected to hold a value in dynamic languages like Ruby and 
Python):

- an integer variable is a variable used to hold an integer;
 
- a string variable is a variable used to hold a string;

- a float variable is a variable used to hold a float; 

- a boolean variable is be a variable used to hold a boolean; 

- for consistency, a class variable should be a variable used to 
  hold a class, e.g.:

  classes = [bool, float, MyClass, Spam, Ham, AnotherClass]
  for cls in classes:  # cls is a "class variable"
      print "The name of the class is", cls.__name__

- and similarly for consistency an instance variable should be a
  variable holding some arbitrary instance. Since everything in an
  instance in Python, this last one is too general to be useful.


Unfortunately people coming to Python from certain other languages, 
particularly Java, (mis)use "class variable" to mean something completely 
different:

- a class variable is a member attached to a class, and therefore 
  shared across all instances, what Python users usually call a 
  "class attribute";

- an instance variable is a member attached to an instance, and 
  therefore *not* shared across instances, what Python users usually
  call an "instance attribute" or even just "attribute".


As I see it, it is not Python being inconsistent. What do you consider is 
inconsistent by *avoiding* the use of "class variable" to mean an 
attribute or member attached to a class?


-- 
Steven



More information about the Python-list mailing list