PDA

View Full Version : Python: self as an argument. Why?


PozestStar
08-12-2012, 10:22 PM
Before someone bops me, yes, I did look this question up here and on Google. The explanations were WAY over my head. I was hoping someone here could (in simple, easy to understand, n00b English) explain to me why we need to/should do this.

Example:
def make_r_wrist_Cntrl(self):
self.r_wrist_Cntrl = pm.circle(r=(1.5), object=True, center = (0,0,0), nr = (1,0,0), n = 'r_wrist_Cntrl')

pm.pointConstraint('r_wrist_JNT', 'r_wrist_Cntrl')

Is the self really necessary? If so, why? If not, why do it?

Thanks for your time.

NaughtyNathan
08-12-2012, 10:59 PM
in as simple n00b terms as possible, no it isn't strictly necessary (in theory) to explicitly declare self (some other languages don't need to) but some convention must be required and pythons design is based on the fact that explicit is better than implicit.

In other words, like bare tabs for indentation, this is just how it is. accept it and move on! :) ;)

giordi
08-12-2012, 11:44 PM
usually I only use self when dealing with classes and inside a procedure i use self in order to access data stored in the class, and in order to achieve that needs to be passed as argument

zoharl
08-13-2012, 04:54 AM
Nathan, I thought I understood why self is necessary, and now you got me confused... ;)

@PozestStar, you provide the self parameter when you need to refer to a specific instance of an object. You probably don't understand what it means, so let's show you why it's necessary. In your example how would you write the function without self? And please provide more details of who calls the function, and how do you use it, or better yet provide a simpler example if you have one.

CGTalk Moderation
08-13-2012, 04:54 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.