14

I am absolutely new to Python (I came from Java) and I have the following doubts about class fields.

Considering code like this:

class Toy():
    def __init__(self, color, age):
        self.color = color
        self.age = age

action_figure = Toy('red', 10)

What is done is clear and very simple:

it is defining a Toy class. The constructor is defining two fields setting their values. Finally (in main) a new Toy instance is created, passing the values of the fields in the constructor call.

In Java to define the same class I do something like this:

public class Toy {
    private String color;
    private int age;

    // CONSTRUCTOR:
    public Dog(String color, int age) {
        this.color = color;
        this.age = age;
    }
}

It is similar, but I have figured out a pretty big difference. In my Java code I declare the class fields as variables outside my constructor. In Python I am defining the class fields directly inside the constructor. So it means that in Java I can declare several fields and use the constructor method to initialize only a subset of these fields, for example something like this:

public class Toy {
    private String color;
    private int age;
    private String field3;
    private String field4;
    private String field5;

    // CONSTRUCTOR:
    public Dog(String color, int age) {
        this.color = color;
        this.age = age;
    }
}

where I also have the field3, field4 and field5 fields that will be not initialized by my constructor. (In case I can set their value subsequently with a setter method.)

Can I do something similar in Python? Can I declare class fields outside the constructor method?

khelwood
  • 55,782
  • 14
  • 81
  • 108
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • Does this answer your question? [Instance variables in methods outside the constructor (Python) -- why and how?](https://stackoverflow.com/questions/38377276/instance-variables-in-methods-outside-the-constructor-python-why-and-how) – Reznik Dec 14 '19 at 16:34
  • 3
    No, **Python doesn't have variable declarations**. Fields come into existence when you assign to them. This doesn't even have to be in `__init__`, it can be in any method or even outside the class – juanpa.arrivillaga Dec 14 '19 at 17:05

6 Answers6

11

Classes in python vs in c++/java are fundamentally different in that a c++/java class has a fixed data structure and size (bytes) because each attribute is declared or defined outside of all methods (normally as private variables) but in python everything is on the fly(dynamically typed).

Choice of defining attribute in constructor vs other methods is about other people being able to quickly understand your code/data structure (although due to dynamic-ness calling python classes data structures would be inappropriate)

As an example of dynamic-ness You can even add new methods and attributes to classes and even instances at run time:

class A:
    pass

Adding stuff to a class at runtime (These will be added to all existing and future instances of the class):

A.key = val

def f(self):
    return 0

A.myfunction = f
a = A()
a.myfunction()
# 0

Adding stuff to a single instance at runtime:

a=A()
a.attr='something'

def f(self):
    return 0

a.fun=f.__get__(a)
a.fun()
# 0
Piyush Singh
  • 2,736
  • 10
  • 26
  • 1
    *"lnstance variables (anything with self. ) can be defined only in methods."*: that is not entirely correct. First, it is not necessary to call the first method argument `self`. Secondly, you can add instance attributes outside of any method: `a = A(); a.myprop = 3` will work just fine. – trincot May 22 '21 at 12:09
7

In python, you could do something like this:

class Toy():
    def__init__(self, color, age):
        self.color = color
        self.age = age

    def another_method(self, f):
         self.field3 = f + 4
         return self.field3

But it is usually recommended for clarity (more arguments here: https://stackoverflow.com/a/38378757/4709400) to initialize all your instance variables inside the constructor, so you would do:

class Toy():
    def__init__(self, color, age):
        self.color = color
        self.age = age
        self.field3 = None
        self.field4 = 0 # for instance
        self.field5 = "" # for instance

    def another_method(self, f):
         self.field3 = f + 4
         return self.field3
stellasia
  • 5,372
  • 4
  • 23
  • 43
  • Ok and there is now way to declare a class field as a variable outside any method? (something like done in my Java example) – AndreaNobili Dec 14 '19 at 16:43
  • 1
    @AndreaNobili Python *doesn't have variable declarations at all* – juanpa.arrivillaga Dec 14 '19 at 17:06
  • 1
    The closest thing Python has to declaring (rather than defining) an instance attribute is the approach taken by dataclasses, but even that is just a slightly different use of a `class` statement to autogenerate an `__init__` method. – chepner Dec 14 '19 at 18:04
4

There isn't really a need for this in Python. What you would call "instance variables" in Java can be added to an instance of a class whenever it is desired:

class Person:

    def __init__(self, name):
        self.name = name

    def get_a_job(self):
        self.job = "Janitor"
        print(f"{self.name} now has a job!")

p1 = Person("Tom")
p2 = Person("Bob")

p1.get_a_job()
print(p1.job)

print(p2.job)

Output:

Tom now has a job!
Janitor
Traceback (most recent call last):
  File "...", line 17, in <module>
    print(p2.job)
AttributeError: 'Person' object has no attribute 'job'
>>> 
Paul M.
  • 10,481
  • 2
  • 9
  • 15
2

As Python is dynamically typed, you don't declare variables beforehand, but they are initialised at runtime. This also means that you don't have to add instance attributes to the constructor, but you can add them any time later. In fact, you can add attributes to any object, including the class object itself. Adding instance attributes to the constructor is mostly a question of consistency and readability.

Data attributes that are added to the class definition are called class attributes in Python (I don't know Java, but I believe, this corresponds to static variables). This is useful e.g. to keep track of all class instances:

class Dog:
  lineage = {'Phylum':'Chordata', 'Class':'Mammalia', 'Species':'Canis lupus'}
  all_dogs = []

  def __init__(self, fur_color, tail_length):
    self.fur_color = fur_color
    self.tail_length = tail_length
    self.all_dogs.append(self)  # class attributes can be accessed via the instance

Bello = Dog('white',50)
print(Dog.all_dogs)
print(Dog.[0].fur_color)
sammy
  • 857
  • 5
  • 13
2

As you noted this is not part of the core python language.

This felt like a missing feature to me as well, for several reasons:

  • readability/maintainability: with the classic python way of defining attributes in the constructor or elsewhere dynamically, it is not obvious when reading the code what is the "contract" (or expected duck contract at least) of an object.

  • compacity: creating long constructors with just self.<foo> = <foo> is not the most fun, and the more fields you need the more lines you have to write

  • ability to extend the contract of a field, for example to add a default value factory in case the default value is mutable, or to add value validators

  • ability to create mix-in classes, i.e. classes that implement some functionality relying on some fields, but not forcing the use of any constructor.

This is why I created pyfields. With this library, each field is defined as a class member:

from pyfields import field
from typing import List

class Toy:
    color: str = field(doc="The toy's color, a string.")
    age: int = field(doc="How old is this Toy. An integer number of years.")
    field3: str = field(default='hello', check_type=True)
    field4: List[str] = field(default_factory=lambda obj: ['world'])
    field5: str = field(default=None, 
                        validators={'should be 1-character long': lambda x: len(x) == 1})

    def __init__(self, color, age):
        self.color = color
        self.age = age


t = Toy(color='blue', age=12)
print(t.field3 + ' ' + t.field4[0])
print(t.field5 is None)
t.field5 = 'yo'

yields

hello world
True
Traceback (most recent call last):
  ...
valid8.entry_points.ValidationError[ValueError]: Error validating [Toy.field5=yo]. InvalidValue: should be 1-character long. Function [<lambda>] returned [False] for value 'yo'.

Note that I use the python 3.7+ type hint syntax above but pyfields is compliant with older versions (python 2, python 3.5), see documentation.

You can even simplify this example further by auto-creating the constructor or using @autofields to generate the field() calls for you. pyfields also provides an @autoclass so you can even generate other class behaviours easily such as string representation, equality, conversion to dict, etc. See autoclass doc.

Note that pyfields was inspired by giants such as attrs, but is distinctive in that it preserves the segregation principle. So it does not fiddle with __init__ or __setattr__ behind your back. This therefore allows your fields to be validated on set (and not only in the constructor), and also to develop elegant mix-in classes defining fields and methods, but no constructor.

smarie
  • 4,568
  • 24
  • 39
  • 1
    Could the negative voter please explain the reason why he/she thinks that this answer does not answer the original question ? That would help other readers. – smarie Aug 09 '20 at 12:29
2

The desired behavior is provided since Python 3.7 by the @dataclass decorator.

from dataclasses import dataclass
from typing import Optional

@dataclass
class Toy1:
    color: str
    age: int
    field3: Optional[str] = None
    fielg4: Optional[str] = None
    field5: Optional[str] = None

    def __init__(self, color:str, age: int) -> None:
        self.color = color
        self.age = age

toy1 = Toy('red', 2)

The constructor can even be suppressed and the behavior is the same.

@dataclass
class Toy2:
    color: str
    age: int
    field3: Optional[str] = None
    fielg4: Optional[str] = None
    field5: Optional[str] = None

toy2 = Toy('red', 2)

@dataclass will enable other function like a nice string representation for the instance (__repr__): Toy(color='red', age=2, field3=None, fielg4=None, field5=None)

or a direct comparison between the instances:

toy1 == toy2
True

A comprehensive explanation as well as the rational behind it can be found at: https://realpython.com/python-data-classes/

If you want to enforce the types during runtime, pydantic can be used in this model.