2

I am just playing around with the language, but wonder if it is possible to use functions defined within a scope of the class without explicitly defining self as the first argument.

I understand the "proper" way to implement a class might be

class minimal:
    variable = 1    
    def add(self,x,y):
        return x+y
    def __init__(self,x):
        self.value = self.add(x,self.variable)

m = minimal(1)
print m.value
--> 2

However, if I define and apply add in a similar way as variable (in the scope of the class), then I get an error (expected):

class minimal:
    variable = 1
    def add(x,y):
        return x+y
    def __init__(self,x):
        self.value = self.add(x,self.variable)

m = minimal(1)
print m.value
--> TypeError: add() takes exactly 2 arguments (3 given)

Is there a way around this? Or this is it generally advised that everything be defined with explicit reference to self (i.e. self.variable=1 defined in the __init__ method and add defined with self as first argument)?

Edit __init__ method corrected to assign to self.value in the second case instead of trying to return a value (unintentional).

0

3 Answers 3

4

Yes, but don't.

Sign up to request clarification or add additional context in comments.

3 Comments

this is great, thanks, but the second article says: "But do note that calling a classmethod involves an additional memory allocation that calling a staticmethod or function does not." Doesn't that argue for using the @staticmethod approach you linked to first?
You missed the third paragraph.
That is precisely how I thought of it when I saw the question. Sure, it's possible, but please don't. And that Python Is Not Java article is about the best on the topic.
4

The name of the first argument is arbitrary. The object is always passed as the first argument. When you declare add, the object will be passed as x. When you call self.add(x,self.variable), self is bound to x in add, x in __init__ is bound to y in add and self.variable is passed as a third argument, which add isn't declared to take.

class minimal:
    variable = 1
    def add(x,y, *args):
        print('x: %s;\ny: %d;\nargs: %s;\n' % (x, y, args))
    def __init__(self,x):
        self.add(x,self.variable)
>>> minimal(2)
x: <__main__.minimal object at 0x1006e6e90>;
y: 2;
args: (1,);

In summary, even if you do away with self as the first argument, you still get the object passed as the first argument to methods.

1 Comment

Thanks for the clarification -- I understand that this was the nature of the error statement above. Somewhat surprising behavior...
1

You can mark the method as being static, try this:

class minimal:
  variable = 1

  @staticmethod
  def add(x,y):
    return x+y

  def __init__(self,x):
    print self.add(x,self.variable)

m = minimal(1)

Though I changed this so your init method doesn't return a value, which it shouldn't.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.