0
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.

MillerTime
  • 317
  • 2
  • 11
  • `y=self.x` needs the `self` to exist outside the instance which is a "contradiction" – cards Jul 03 '22 at 20:39
  • Why are you passing a class attribute as a pre-defined argument to the same class's method? Just remove it as an argument and recall it directly within the scope of the function. – aliwimo Jul 03 '22 at 20:41

2 Answers2

0

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)
ShivaGaire
  • 2,283
  • 1
  • 20
  • 31
0

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
slothrop
  • 3,218
  • 1
  • 18
  • 11