473,788 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

to thine own SELF be true...

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=getop t.getopt(args,c mdopts[cmd][0], cmdopts[cmd][1])

but I'm having to type:

self.optc,self. args=getopt.get opt(self.args,s elf.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

--
Mark Harrison
Pixar Animaion Studios
May 5 '06 #1
5 1395
Mark Harrison wrote:
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.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
May 5 '06 #2
Mark Harrison wrote:
For example, in my brain I'm thinking:

optc,args=getop t.getopt(args,c mdopts[cmd][0], cmdopts[cmd][1])

but I'm having to type:

self.optc,self. args=getopt.get opt(self.args,s elf.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(a rgs, opts, longopts)

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

or in other words a lot fewer uses of self.
May 5 '06 #3
Mark Harrison wrote:
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=getop t.getopt(args,c mdopts[cmd][0], cmdopts[cmd][1])

but I'm having to type:

self.optc,self. args=getopt.get opt(self.args,s elf.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
May 5 '06 #4
On Fri, May 05, 2006 at 05:08:24PM +0000, Mark Harrison wrote:
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

--
mouse, n: a device for pointing at the xterm in which you want to type.
-- Fortune
Visit me on the Web: http://www.elehack.net
May 5 '06 #5
Michael Ekstrand wrote:
On Fri, May 05, 2006 at 05:08:24PM +0000, Mark Harrison wrote:
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


This fits perfectly with "Explicit is better than implicit."
May 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
17085
by: Grzegorz Staniak | last post by:
Hello, I'm a newbie Python user, a systems administrator - I've been trying to switch from Perl to Python for administrative tasks - and one thing I cannot understand so far is why I need the special 'self' (or anything esle) argument in class method definitions. I might have missed an explanation in the docs, a quick Google search did not help. Is there somewhere on the web you could direct me to? Thanks,
2
9645
by: Jim Jewett | last post by:
Normally, I expect a subclass to act in a manner consistent with its Base classes. In particular, I don't expect to *lose* any functionality, unless that was the whole point of the subclass. (e.g., a security-restricted version, or an interface implementation that doesn't require a filesystem.) One (common?) exception seems to occur in initialization. I understand stripping out arguments that your subclass explicitly handles or...
15
2598
by: Ralf W. Grosse-Kunstleve | last post by:
****************************************************************************** This posting is also available in HTML format: http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html ****************************************************************************** Hi fellow Python coders, I often find myself writing:: class grouping:
4
8364
by: Wow | last post by:
when calling a object in an html, should I use self.objectname this.objectname or document.objectname? for example, i have a form called theform and a link called thelink i can call document.theform, but not document.thelink i can use both this.theform and this.thelink and self.theform and self.thelink.
3
1202
by: 63q2o4i02 | last post by:
Hi, I was wondering how I may get a python function to know what its name is without me having to write it manually? For example: def func1(): <do some stuff1> print 'func1' return True def func2(): <do some stuff2>
24
2302
by: Peter Maas | last post by:
The Python FAQ 1.4.5 gives 3 reasons for explicit self (condensed version): 1. Instance variables can be easily distinguished from local variables. 2. A method from a particular class can be called as baseclass.methodname(self, <argument list>). 3. No need for declarations to disambiguate assignments to local/instance variables.
14
5118
by: Gert Cuykens | last post by:
Is there a difference between <code> class HelloWorld: def index(self): index.exposed = True return "Hello world!" </code> and
26
2659
by: hide1713 | last post by:
HI I'm currently using Python. I find that a instance variable must confined with self, for example: class a: def __init__(self): self.aa=10 def bb(self): print self.aa # See .if in c++,I could use aa to change that variable
84
7223
by: braver | last post by:
Is there any trick to get rid of having to type the annoying, character-eating "self." prefix everywhere in a class? Sometimes I avoid OO just not to deal with its verbosity. In fact, I try to use Ruby anywhere speed is not crucial especially for @ prefix is better- looking than self. But things grow -- is there any metaprogramming tricks or whatnot we can throw on the self? Cheers,
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.