4 ms·
Note that the name "self" is a convention, not a requirement. If it's just the amount of keystrokes that you're concerned about, name it something else like "o"
by fred256 12y ago
Note that the name "self" is a convention, not a requirement. If it's just the amount of keystrokes that you're concerned about, name it something else like "o".
Of course, good luck having someone else understand your code...
- anindyabd 12y agoDid not know this. Thought self was equivalent to this in Java. Mind = blown.
- raverbashing 12y agoSee, you pass it as a parameter So it can be called anything you want BUT DON'T DO IT! Because you'll break the convention and anyone who maintains your code after that will hate you.
- eatonphil 12y agoYou can ALSO declare a class member function in a different file. Since it just accepts self as the first parameter... It still works.
- abecedarius 12y agoPeter Norvig once proposed using '_' but got shot down.
- kevin_thibedeau 12y ago'_' is already idiomatic for throwaway variables. It wouldn't help matters to use it for OOP.
- raymondh 12y agoIn the Python world, '_' is used in three different ways: * As a throw-away variable: >>> a, b, _, c, d = f(t) * As a gettext call: >>> print _('Welcome to Python') * And at the interactive prompt as the result of the previous expression: >>> 3 + 4 7 >>> _ * 10 70
- pluma 12y agoAs most of my Python involves localized Django, I twitch every time I see `_` used for anything other than some variation of gettext.
- lazzlazzlazz 12y agoWeird, since the gettext use-case is definitely the least idiomatic and least common use of `_`.
- pluma 12y agoAs I said, most of my Python involves localized Django. So gettext is used everywhere and (apart from the REPL) it's the other uses of `_` that are the odd ones out.
- Walkman 12y agothat's why you should use '__' as throw-away variable (two underscores)
- bobisme 12y agoWow. How did I go years without knowing this about the repl?
- collyw 12y agoIts a really crap variable name as well. I posted on Stack Overflow asking what it was doing, assuming it was some Python syntax, only to be pointed to the import at the top of the file, which was being renamed to "_". Give variables meaningful names please.