Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
too much "self."
#1
lots of code has self. in so many places. what are good ways to reduce that? i hate typing the same thing so many times and have assigned things to local variables just to avoid that. can i use s. or _. instead of self. (with similar in the arguments)?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
You can use whatever you want. Whatever you call the first parameter in a bound method will be whatever self is. It's just a convention to call it self.

That said, it's a good convention. Variable names should be description, which self is. Single letters, like s and _, are not descriptive. And self is probably the most widely used convention of Python programming, that you will just be confusing other Python programmers if you don't use it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
how about a mechanism where i type it in like .. and another script "processes it" to change .. to self.?

how often do you type in "self."?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
(Apr-14-2019, 01:55 AM)Skaperen Wrote: how about a mechanism where i type it in like .. and another script "processes it" to change .. to self.?
how often do you type in "self."?

I type in "self." whenever I have a class, that has methods with parameters. It's just the convention for what you call the first parameter in a bound method, but you can call it "notself." if you want, as long as the parameter has the same name. In other languages you might be typing "this" instead of "self"
Reply
#5
self specifies which namespace the variable or function is in. If you only use a variable once, then self is not necessary, because it can exist in a local namespace and not in the class instance's namespace. That being said, keying in self is much more helpful than trying to keep track of some number of variables that you don't know the scope / namespace of. So, if you are using Tkinter for example, using import tkinter as tk, you would know that "self.variable" is in the instance's namespace, and "tk.variable" is in the Tkinter namespace.
Reply
#6
in many languages there is no special name. the context is implied, somewhat like if self and global were combined.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
(Apr-14-2019, 01:55 AM)Skaperen Wrote: how often do you type in "self."?

Almost always. Sometimes I forget, and I'll do a copy and paste. Programming is typing. If you can't handle typing, take up knitting or something.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
(Apr-14-2019, 12:23 PM)ichabod801 Wrote:
(Apr-14-2019, 01:55 AM)Skaperen Wrote: how often do you type in "self."?

Almost always. Sometimes I forget, and I'll do a copy and paste. Programming is typing. If you can't handle typing, take up knitting or something.

i disagree. typing is one form of programming, still subject to optimization.

i first saw another form back in the late 1970s, which involved putting block components together as a way of expressing logic for algorithms and programming. the ideas i have seen since then involve many ways to express ideas, many of which did involve a conversion to text expression forms as part of the process. i have put some thought into IDEs like this where programmer keypresses end up as code, but it isn't literally the code that was typed.

i have also seen "drag and drop programming" though that comes across to me as cumbersome for code, but probably great for web page layout (produces HTML, which many consider to be code).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020