473,807 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Too many 'self' in python.That's a big flaw in this language.

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

That's a big inconvenience in coding ,especially when you have lot of
variable
If you method need 10 variables ,you have to type "self" for 10 times
and that also makes your variable longer.
>From My point,I think this only help python interpreter to deside
where to look for.
Is there anyone know's how to make the interpreter find instance name
space first?
Or any way to make programmer's life easier?

Jun 27 '07
26 2663
On Jun 27, 5:02 am, "hide1...@gmail .com" <hide1...@gmail .comwrote:
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

That's a big inconvenience in coding ,especially when you have lot of
variable
If you method need 10 variables ,you have to type "self" for 10 times
and that also makes your variable longer.
From My point,I think this only help python interpreter to deside

where to look for.
Is there anyone know's how to make the interpreter find instance name
space first?
Or any way to make programmer's life easier?

Guido has already said that this will not change in Python 3.0 See PEP
3099.

John Roth

Jun 27 '07 #11
Jorgen Bodde a écrit :
I had the same feeling when I started, coming from a C++ background, I
forgot about self a lot, creating local copies of what should be an
assign to a class instance, or methods that could not be found because
I forgot 'self' .

Now I am 'kinda' used to it, as every language has some draw backs
(you can't please all). But, what about something in between like only
using the dot (.) for a shorter notation?

self.some_var = True

Could become:

.some_var = True

Which basically shows about the same thing, but you leave 'self' out
of the syntax. Ofcourse it should not be allowed to break a line
between the dot and the keywords, else Python would never know what to
do;

my_class()
.my_var = True

Should not be parsed the same as;

my_class().my_v ar = True

Just a suggestion. I am pretty happy with self, but I could settle for
a shorter version if possible.
What is nice with the required, explicit reference to the instance -
which BTW and so far is not required to be *named* 'self' - is that it
avoids the need for distinct rules (and different implementations ) for
functions and methods. The different 'method' types are just very thin
wrappers around function objects. Which in turn allow to use 'ordinary'
functions (defined outside a class) as methods - IOW, to dynamically
extend classes (and instances) with plain functions. Uniformity can also
have very practical virtues...
Jun 27 '07 #12
Bruno Desthuilliers wrote:
Jorgen Bodde a écrit :
>But, what about something in between like only
using the dot (.) for a shorter notation?
How about "Mavis Beacon Teaches Typing"?

John Nagle
Jun 27 '07 #13
On Jun 27, 2:54 pm, John Nagle <n...@animats.c omwrote:
But, what about something in between like only
using the dot (.) for a shorter notation?

How about "Mavis Beacon Teaches Typing"?
How about no "wouldn't it be better" suggestions until at least three
months after the suggester has written at least 1000 lines of working
code.?

Jun 27 '07 #14
Bjoern Schliessmann <us************ **************@ spamgourmet.com >
wrote:
...
Mh, strange, I personally like to use "this.a" in C++, to make clear
I use an instance variable.
That would be nice, unfortunately your C++ compiler will refuse that,
and force you to use this->a instead;-).

Many programming shops use naming conventions instead, such as my_a or
a_ (trailing underscore for member-variables) -- I've even seen the
convention this_a which IMHO is silly (at that point you might as well
use this->a and avoid the 'convention'!-).

Anyway, I essentially agree with you (except for the C++ bit: since this
is a pointer, it needs ->). However, full disclosure, Smalltalk/XP
superstar Kent Beck disagrees -- in his good book "Test Driven Design by
Example", in the chapter where he gives the Python example, he DOES
whine against the need to explicitly say self (the one bad bit in the
book:-).

For the curious: the explicit-self idea is essentially taken from
Modula-3, a sadly now forgotten language which still had an impact on
the history of programming.
Alex
Jun 28 '07 #15
Alex Martelli wrote:
Bjoern Schliessmann <us************ **************@ spamgourmet.com >
wrote:
>Mh, strange, I personally like to use "this.a" in C++, to make
clear I use an instance variable.
That would be nice, unfortunately your C++ compiler will refuse
that, and force you to use this->a instead;-).
Sure, thanks. Before I last used C++ I was forced to use Java --
where I would write "this.<member>" . ;)
Many programming shops use naming conventions instead, such as
my_a or a_ (trailing underscore for member-variables) -- I've even
seen the convention this_a which IMHO is silly (at that point you
might as well use this->a and avoid the 'convention'!-).
ACK.
For the curious: the explicit-self idea is essentially taken from
Modula-3, a sadly now forgotten language which still had an impact
on the history of programming.
Mh, I'm going to read some about this one.

Regards,
Björn

--
BOFH excuse #4:

static from nylon underwear

Jun 28 '07 #16
In article <ma************ *************** ***********@pyt hon.org>,
"Jorgen Bodde" <jo************ *@gmail.comwrot e:
I had the same feeling when I started, coming from a C++ background, I
forgot about self a lot, creating local copies of what should be an
assign to a class instance, or methods that could not be found because
I forgot 'self' .

Now I am 'kinda' used to it, as every language has some draw backs
(you can't please all). But, what about something in between like only
using the dot (.) for a shorter notation?

self.some_var = True

Could become:

.some_var = True

Which basically shows about the same thing, but you leave 'self' out
of the syntax. Ofcourse it should not be allowed to break a line
between the dot and the keywords, else Python would never know what to
do;

my_class()
.my_var = True

Should not be parsed the same as;

my_class().my_v ar = True

Just a suggestion. I am pretty happy with self, but I could settle for
a shorter version if possible.

- Jorgen
Hmmm... I like this idea. Would you put a dot in the argument of a
class method?

def afcn(.,x,y):
# stuff here

??

I still like it. self remains a wart on python for me after 5 years of
use despite a deep love of the language and developers' community.

--
-- Lou Pecora

When I was a kid my parents moved a lot, but I always found them.
(R.Dangerfield)
Jun 28 '07 #17
A.T.Hofkamp wrote:
>>>>a = Car2(123)
b = Car2(123)
a == b

True
>>>>set([a,b])

set([Car2(123), Car2(123)])

I get a set with two equal cars, something that never happens with a set
my math teacher once told me.

Then your math teacher misspoke.
You have two different cars in the set,
just as expected. Use `is`.
http://docs.python.org/ref/comparisons.html

This is good behavior.

Cheers,
Alan Isaac
Jun 28 '07 #18
On 2007-06-28, Alan Isaac <ai****@america n.eduwrote:
A.T.Hofkamp wrote:
>>>>>a = Car2(123)
>b = Car2(123)
>a == b

True
>>>>>set([a,b])

set([Car2(123), Car2(123)])

I get a set with two equal cars, something that never happens with a set
my math teacher once told me.


Then your math teacher misspoke.
You have two different cars in the set,
just as expected. Use `is`.
http://docs.python.org/ref/comparisons.html

This is good behavior.
Hmm, maybe numbers in sets are broken then?
>>a = 12345
b = 12345
a == b
True
>>a is b
False
>>set([a,b])
set([12345])
Numbers and my Car2 objects behave the same w.r.t. '==' and 'is', yet I get a
set with 1 number, and a set with 2 cars.
Something is wrong here imho.

The point I intended to make was that having a default __hash__ method on
objects give weird results that not everybody may be aware of.
In addition, to get useful behavior of objects in sets one should override
__hash__ anyway, so what is the point of having a default object.__hash__ ?

The "one should override __hash__ anyway" argument is being discussed in my
previous post.
Albert
Jun 28 '07 #19
In article <sl************ ****@se-162.se.wtb.tue. nl>,
"A.T.Hofkam p" <ha*@se-162.se.wtb.tue. nlwrote:
In object oriented programming, objects are representations of values, and the
system shouldn't care about how many instances there are of some value, just
like numbers in math. Every instance with a certain value is the same as every
other instance with the same value.
Whether two things are equal depends on the context. Is one $10 note equal
to another? It depends.

If the context is a bank teller making change, then yes, they are equal.
What's more, there are lots of sets of smaller notes which would be equally
fungible.

If the context is a district attorney showing a specific $10 note to a jury
as evidence in a drug buy-and-bust case, they're not. It's got to be
exactly that note, as proven by a recorded serial number.

In object oriented programming, objects are representations of the real
world. In one case, the $10 note represents some monetary value. In
another, it represents a piece of physical evidence in a criminal trial.
Without knowing the context of how the objects are going to be used, it's
really not possible to know how __eq__() should be defined.

Let me give you a more realistic example. I've been doing a lot of network
programming lately. We've got a class to represent an IP address, and a
class to represent an address-port pair (a "sockaddr") . Should you be able
to compare an address to a sockaddr? Does 192.168.10.1 == 192.168.10.1:0?
You tell me. This is really just the "does 1 == (1 + 0j)" question in
disguise. There's reasonable arguments to be made on both sides, but there
is no one true answer. It depends on what you're doing.
Jun 28 '07 #20

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

Similar topics

75
3915
by: David MacQuigg | last post by:
Seems like we need a simple way to extend Python syntax that doesn't break existing syntax or clash with any other syntax in Python, is easy to type, easy to read, and is clearly distinct from the "base" syntax. Seems like we could put the @ symbol to good use in these situations. Examples: print @(separator = None) x, y, z @x,y:x*x+y*y -- anonymous function
2
1724
by: Calvin | last post by:
Hi All, Could someone tell me just how secure Python is if compiled to an exe? Is it more or less secure than using some other language? Thanks
15
2601
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:
8
2711
by: ssecorp | last post by:
I first learned about OO from Java. I much prefer to program in Python though. However I am consufed about 2 things. 1. Why do I have to pass self into every method in a class? Since I am always doing why cant this be automated or abstracted away? Are the instances where I won't pass self? I imagine there is some tradeoff involved otherwise it would have been
0
9720
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
9599
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
10626
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
10372
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
10374
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
10112
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
9193
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
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.