to thine own SELF be true...

M

Mark Harrison

Is there a way to do something equivalent to "import * from self"?
Perhaps I'm doing something wrong, but I'm having a headache
when dealing with class instance data, forgetting to always
put the "self." prefix

For example, in my brain I'm thinking:

optc,args=getopt.getopt(args,cmdopts[cmd][0], cmdopts[cmd][1])

but I'm having to type:

self.optc,self.args=getopt.getopt(self.args,self.cmdopts[self.cmd][0],
self.cmdopts[self.cmd][1])


Is there a way to get rid of those the "self." references, or is this
just something I need to get my brain to accept?

Many TIA,
Mark
 
B

bruno at modulix

Mark said:
Is there a way to do something equivalent to "import * from self"? (snip)

Is there a way to get rid of those the "self." references,
No.

or is this
just something I need to get my brain to accept?

Yes.

And FWIW, "from somemodule import *" is usually considered bad style.
 
D

Duncan Booth

Mark said:
For example, in my brain I'm thinking:

optc,args=getopt.getopt(args,cmdopts[cmd][0], cmdopts[cmd][1])

but I'm having to type:

self.optc,self.args=getopt.getopt(self.args,self.cmdopts[self.c
md][0],

self.cmdopts[self.cmd][1])


Is there a way to get rid of those the "self." references, or is this
just something I need to get my brain to accept?
I would wonder why it is that all of those need to be attributes on self.
Are you sure you aren't storing too many things as attributes?

Trying to guess some context around that code, I would expect something
like:

def docommand(self, cmd, args):
opts, longopts = self.cmdopts[cmd]
optc, args = getopt.getopt(args, opts, longopts)

return self.getAction(cmd)(optc, args)

or in other words a lot fewer uses of self.
 
L

Larry Bates

Mark said:
Is there a way to do something equivalent to "import * from self"?
Perhaps I'm doing something wrong, but I'm having a headache
when dealing with class instance data, forgetting to always
put the "self." prefix

For example, in my brain I'm thinking:

optc,args=getopt.getopt(args,cmdopts[cmd][0], cmdopts[cmd][1])

but I'm having to type:

self.optc,self.args=getopt.getopt(self.args,self.cmdopts[self.cmd][0],
self.cmdopts[self.cmd][1])


Is there a way to get rid of those the "self." references, or is this
just something I need to get my brain to accept?

Many TIA,
Mark

The ones without "self." in front of them will die when you leave
the method (e.g. sort of like local variables in a function). If
you want them to be instance attributes the "self." is required.
Now if you use them a lot you can create shortcuts inside the
method. The lack of "self." is how python knows if this is
a temporary local variable (lasts until the method is exited) or
an instance attribute that lasts across methods. Hopefully this
will help.

-Larry
 
M

Michael Ekstrand

Is there a way to get rid of those the "self." references, or is this
just something I need to get my brain to accept?

It's pretty much just something you'll need to get your brain to accept.
You can replace self with something shorter, like s, but that's sure to
cause mass confusion for other people who try to read your code.

I, however, find that the self. references are a great benefit to
readability, as it makes the scope of a variable quite easy to tell. A
lot of my C++ code is littered with `this->' or similar
explicit-scope-description things...

- Michael
 
P

Paul Watson

Michael said:
It's pretty much just something you'll need to get your brain to accept.
You can replace self with something shorter, like s, but that's sure to
cause mass confusion for other people who try to read your code.

I, however, find that the self. references are a great benefit to
readability, as it makes the scope of a variable quite easy to tell. A
lot of my C++ code is littered with `this->' or similar
explicit-scope-description things...

- Michael

This fits perfectly with "Explicit is better than implicit."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top