1

According to my understanding the data members of objects in Python are referred to as 'attributes'. Attributes that are callable are referred to as an object's 'methods', but I couldn't find a name for non-callable attributes, such as val in the following example:

class C:

    def __init__(self):
        self.val = 42. # How would this be called?

    def self.action():
        """A method."""
        print(self.val)

I am sure different people may call val different things like 'field' or 'variable' but I am interested in an official name.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
mrclng
  • 483
  • 2
  • 14
  • Why do you need an official name? – Quelklef Jun 17 '18 at 19:50
  • if I want to talk to others about, search for or otherwise reference certain constructs in a programming language I try to be as precise as possible. I believe the best way to do that is to use terms that are official – mrclng Jun 17 '18 at 20:37

2 Answers2

3

Surprisingly hard to find official information on this topic. After reading this article I do believe it should simply be called Class Variable and Instance Variable.


Attributes, Properties, Methods and Variables

Attribute is the collection name for the three names Property, Method and Variable. The latter two are prefixed by either Class or Instance. A property can only belong to the Class.

enter image description here

class Foo:
    a = 1
    def __init__(self):
        self.b = 2

    @property
    def c(self):
        return 3

    @classmethod
    def d(cls):
        return 4

    def e(self):
        return 5

Foo.a    # Class Attribute:      Class Variable
Foo().a  # Class Attribute:      Class Variable

Foo().b  # Instance Attribute:   Instance Variable

Foo.c    # Class Attribute:      Property

Foo.d    # Class Attribute:      Class Method
Foo().d  # Class Attribute:      Class Method

Foo.e    # Class Attribute:      Class Method
Foo().e  # Instance Attribute:   Instance Method

Sources

Diagram made in Creately

Mandera
  • 2,647
  • 3
  • 21
  • 26
-1

I'm not sure if one exists, but I'd suggest just "instance attribute".

Features about this naming:

  1. It excludes methods. Methods are all callable class attributes, so this wording excludes all methods.
  2. It includes callable instance attributes. Consider the following code:
class Container:
    def __init__(self, item):
        self.item = item

c = Container(x)
c.item  # is an "instance attribute"
c.item == x  # True

Note that c.item is an "instance attribute" regardless of whether or not it's callable. I think this is behaviour you're after, but I'm not sure.

  1. It excludes non-callable class attributes, e.g.
class SomeClass:
    x = 5  # Is not an "instance attribute"
  1. It includes per-instance attributes, e.g.
obj.x = 5
obj.x  # Is an "instance attribute"

In the end, all of these features may be positives or negatives depending on specifically what you want. But I don't know specifically what you want, and this is as close as I can get. If you can provide more information, I can give a better suggestion.

Quelklef
  • 1,999
  • 2
  • 22
  • 37
  • I don't think that this is a good idea as `Attribute` is the collection name. Quote from the docs: [_'By the way, I use the word attribute for any name following a dot...'_](https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces) – Mandera Nov 19 '20 at 16:53