Argument Preset to Class Variable in Python

90 Views Asked by At
class Example:

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

    def function(self, y=self.x):
        pass

Example(72)

When I run this code, I get the following error.

Traceback (most recent call last):
  File "/home/millertime/Desktop/example.py", line 1, in <module>
    class Example:
  File "/home/millertime/Desktop/example.py", line 7, in Example
    def function(self, y=self.x):
NameError: name 'self' is not defined

Evidently, Python isn't happy with having an argument preset to a class variable. Is there a proper way to do this? I know the class info is being passed in the first argument, self. Any way to reference this further along to make my code possible? I've tried changing y=self.x to y=x, but as I suspected this just threw a NameError. I'm well aware of other ways to do this inside the function, but I'm interested if it's possible or not.

2

There are 2 best solutions below

0
ShivaGaire On BEST ANSWER

In python, the expressions in default arguments of functions are calculated when the function is defined, not when it's called. In the case above there won't be any instantiation hence the error.

You can do this instead

class Example:

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

    def function(self, y=None):
        if y is None:
           y=self.x
        pass

Example(72)
0
slothrop On

This works, though it's not necessarily more palatable than the other ways to do it inside the function that you mention:

class Example:
  def __init__(self, x):
    self.x =x

  def _function(self, y):
    return y * self.x

  def normal_func(self, y):
    return y + self.x

  def __getattr__(self, attr):
    if attr == 'function':
      return lambda y=self.x: self._function(y)
    return super().__getattr__(self, attr)

a = Example(5)
print(a.function(10))   # should return 10 * 5
print(a.function())     # should return 5 * 5
print(a.normal_func(7)) # should return 7 + 5

prints:

50
25
12

Note that you might not get what you expect if you hold a reference to the function object, then execute it later:

f = a.function  # This instance of the function has default y=5
a.x = 8         # This doesn't change default y in our stored f
print(f())      # So this gives 5 * 8

prints:

40