Darren Young Darren Young - 1 year ago 151
Python Question

Python 'self' keyword

I am new to Python (usually work on C#), started using it over the last couple of days.

Within a class, do you need to prefix any call to that classes data members and methods? So if I am calling a method or obtaining a value from that class, from within that class, I need to use self.method(), or self.intvalue, for example?

I just want to check that there isn't a less verbose way that I have not encountered yet.

Answer Source

There is no less verbose way. Always use self.x to access the instance attribute x. Note that unlike this in C++, self is not a keyword, though. You could give the first parameter of your method any name you want, but you are strongly advised to stick to the convention of calling it self.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download