1

I was debugging some python code and I came across a bunch of instance methods declared without self. I was curious as to what happens here. Does self get assigned to the first variable declared here?

The code is something like shown below.

class myClass:
 def myFunc (var1, var2):
 return var2

Here is self assigned to var1 ?

asked Aug 7, 2014 at 18:59
2
  • 2
    Why not print var1 and find out? Commented Aug 7, 2014 at 19:07
  • @jonrsharpe : Thanks. Should have done that. Did not think of that. Printing does confirm what people have answered here. Commented Aug 7, 2014 at 19:12

2 Answers 2

4

Yes, self (that is, the instance) will be assigned to the first variable (assuming there is no decorator like classmethod or staticmethod).

answered Aug 7, 2014 at 19:00
Sign up to request clarification or add additional context in comments.

Comments

2

Your code snippet is equivalent to

class myClass:
 def myFunc(self, var2):
 return var2

The first parameter is passed the instance; name is irrelevant.

answered Aug 7, 2014 at 19:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.