Any ideas about how to get rid of self? [closed]
Asked Answered
S

3

7

Is there anyway of making Python methods have access to the class fields/methods without using the self parameter?

It's really annoying having to write self. self. self. self. The code gets so ugly to the point I'm thinking of not using classes anymore purely for code aesthetics. I don't care about the risks or best practices. I just want not to see self anymore.

P.S. I know about the renaming possibility but it's not the point.

Scranton answered 6/2, 2013 at 11:6 Comment(15)
Any ideas about how to get rid of self? - sounds funnyMourant
How will your instances know what they're operating on?Factorage
you could name the parameter s, other or not_self. :-pSabbath
It's a dynamic language. How hard is it to search for a method/variable in the class scope?Scranton
@C2H5OH I feel a new PEP coming on...Sabbath
@Sabbath my comment was removed due to obscenity I guess :-(Casebook
What have you tried that didn't work? Have you looked at horribly ugly locals() hacks?Semiweekly
How would you assign values to attributes on self without explicit self?Sallet
Well, I guess it will have to be something like when you declare them you would have to write self, but when you use them (99% of time) you would just forget the self part. I mean... who would wanna use the same name as a field in a local method variable?Scranton
But you don't declare attributes in Python... or are you thinking of something else?Sallet
I don't know if "declare" is the right word but when you write in the class scope attribute = 1, I consider that declaring. When you use them as self.attribute in the methods I would like not to use self anymore. If would be possible to implement this in Python that's how I would do it. Maybe I care to much about how the code looks...Scranton
I've never had this problem; I'll refer to an instance attribute a couple of times in most methods, rarely more. If you're accessing a lot of instance variables, perhaps you should be storing them in some data structure which you access once (e.g. a dict of options)?Alurta
Well it depends what you understand by couple of times. Even once is to much for me. I prefer to see context[context_key] = {} instead of self.context[self.context_key] = {}Scranton
But the problem with attribute = 1 in class scope is that this doesn't declare an instance attribute -- it declares a class attribute, which is different. Instance attributes in Python are not declared statically, they are dynamic.Sallet
This python aspect is really annoying. A bounty to get around this should be linked to this thread.Pretorius
C
2

No, there isn't. You could though use another word instead of self, although the convention is to use "self".

Calix answered 6/2, 2013 at 11:13 Comment(0)
C
5

This blog by Guido explains why it's here to stay: http://neopythonic.blogspot.co.uk/2008/10/why-explicit-self-has-to-stay.html

Cupro answered 6/2, 2013 at 11:13 Comment(4)
I don't care why it has to stay. I want it gone :))Scranton
@AlBundy I think you'd have to look at another language then. It's as ingrained as whitespace denoting a block of codeCupro
I like Python especially for the whitespace blocking. I like writing less code. Less is by default more clear. I mean looking at a method and seeing self every two words... I don't know.Scranton
The linked blog doesn't address this at all; it's talking about removing self from parameter lists and making it a keyword; even if that happened, the actual method code would still be loaded with the selfs OP dreads, and then you wouldn't even be able to use a different name.Semiweekly
S
3

The only possible solution (except for making your own no-self Python version (using sources))
Try another language.

Scrambler answered 6/2, 2013 at 11:14 Comment(0)
C
2

No, there isn't. You could though use another word instead of self, although the convention is to use "self".

Calix answered 6/2, 2013 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.