473,795 Members | 3,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python 2.4: Why only assignments to None are forbiden?

Hi,

Textually from the highlights of python 2.4:

"Assigning to None - the compiler now treats assigning to None as a
SyntaxError."

I think in general assignments to built-in types, functions, and
variables should be also forbiden. It's a common mistake to do things
like this:
def getFileName(fil e):

.... parts=file.spli t('/')
.... return parts('/')[-1]

Specially if you come from python 2.1.x where "file" didn't exist.
Instead, there was "open"

On the example's context, file is a string and won't cause any damage
because it is inside a function, so, the scope is local and it will be
deleted after the function call. But think what would happen if somebody
defines "file" as a global variable and other people use that code? For
the author won't be any consequences at all, because if s/he does this,
it means that probably s/he isn't working with files, but if somebody
else takes that code and doesn't see this, then s/he will invest some
time trying to find the bug.

Doing this validation for python 2.4.x will break some things, like the
validation with "None" does. But I think it's better when you now that
you are trying to use a "reserved word", you could as well use the "str"
as an example, which I think is also common on newies.

Regards,
Josef
Jul 18 '05
15 1867
Peter Hansen wrote:
One perfectly good reason to assign to builtins (and there are others,
I'm sure) is to provide a "mock" function to replace a standard
one, for use during automated testing.


That's understandable, but it doesn't work for None. At the C
level there is a Py_None object which is the same singleton object
used by None. If you replace the builtin None with a mock None
then almost certainly there will be strange effects.

Eg,

def spam():
print "Here there be Vikings"

x = spam()

At this point, x contains the real None and not the mock one.

class MyList(list):
def get(self, item):
if item.startswith ("__"):
raise AccessDenied(it em)
return list.get(item)

x = MyList().get("w ensleydale")
Again, x is the real None object and not the mock one.

So if reasonable code did the following

if x is None:
print "Sorry, not even wensleydale"
else:
print "Yes, we have it"

then it would break under the mock None case.

In theory it could be possible to make this work. Setting
None in builtins could change some sort of proxy object at
the C level, and ... though the object ids wouldn't be right.
Hmmm. It would be a lot of work for very, very little gain.

So while we do have

Special cases aren't special enough to break the rules.

it's more than trumped by

Although practicality beats purity.

It's almost impossible to make a proxy None object work
correctly.

In a related topic, would you like to have a proxy for the
numbers 0, 1, 2, etc.? Is None special because it's a
variable name that cannot be reassigned or is it not special
because it's just like other builtin objects that cannot
be replaced by user-defined objects?

Andrew
da***@dalkescie ntific.com
Jul 18 '05 #11

"Andrew Dalke" <ad****@mindspr ing.com> wrote in message
news:ei******** ***********@new sread1.news.pas .earthlink.net. ..
Peter Hansen wrote:
One perfectly good reason to assign to builtins (and there are others,
I'm sure) is to provide a "mock" function to replace a standard
one, for use during automated testing.


That's understandable, but it doesn't work for None.


Peter was not suggesting that replacement of None be allowed but that
replacement of things like file() should continued to be allowed and not
prohibited as someone (I believe other than the OP) suggested.

Terry J. Reedy

Jul 18 '05 #12
Terry Reedy wrote:
Peter was not suggesting that replacement of None be allowed but that
replacement of things like file() should continued to be allowed and not
prohibited as someone (I believe other than the OP) suggested.


Ahh, thank you. I didn't catch the shift in topic.

Andrew
da***@dalkescie ntific.com
Jul 18 '05 #13
Bengt Richter wrote:
Since this is a compile-time issue , perhaps it is time to consider
a pragma statement as a framework for telling python pre-runtime things
like optimization info? E.g., pragma could send in-context meta-info
to various python infrastructure components using python method call syntax
which the compiler could recognize in the token stream and compile and
execute in its working environment right then. So e.g.,

pragma compiler.overri de(len=True, cmp=False)


There's a recipe for doing something very similar to this with the
current Python:

http://aspn.activestate.com/ASPN/Coo.../Recipe/277940

--
Ian Bicking / ia**@colorstudy .com / http://blog.ianbicking.org
Jul 18 '05 #14
On Mon, 15 Nov 2004 00:35:12 -0600, Ian Bicking <ia**@colorstud y.com> wrote:
Bengt Richter wrote:
Since this is a compile-time issue , perhaps it is time to consider
a pragma statement as a framework for telling python pre-runtime things
like optimization info? E.g., pragma could send in-context meta-info
to various python infrastructure components using python method call syntax
which the compiler could recognize in the token stream and compile and
execute in its working environment right then. So e.g.,

pragma compiler.overri de(len=True, cmp=False)


There's a recipe for doing something very similar to this with the
current Python:

http://aspn.activestate.com/ASPN/Coo.../Recipe/277940

Yes, that's a nice clever decorator thing for certain kinds of optimization
(by Raymond Hettinger), but if file='something ' were disallowed, and you
wanted to do it in a function, I don't think a decorator would get
control early enough to do anything about it, even if there were a hook
for unlocking the bindability of file. But yes, in general, I can see why you
were reminded ;-)

Regards,
Bengt Richter
Jul 18 '05 #15
I'm seeing Python mentioned a bit more in "help wanted" job descriptions, reflecting, I suppose, a modicum of inroads Python is making into various development or IT environments.

As I contemplated a posted job for a friend who meets the requirements, save the mention of desired Python competence, it occurred to me that it wouldnot be hard for him (a competent SW engineer) to become competent in Python; in fact, that's a real Python strong point.

If we see Python increasingly in help wanted ads, one might expect competent job seekers to take the week or two it would take to get waist deep in Python (or do I over-estimate: is it more like two days for a competent SW engineer to learn and get practiced in Python?)

Python is so easy to learn, it's a natural choice for someone wanting to gain a broader skillset; when breadth of language knowledge becomes important, Python is an easy target.

If the pick-up of Python as an "extra" language becomes a trend, then I believe the tipping point will be reached, and a positive feedback loop will see the language adopted and employed much more widely, and blossom even further in the hands of a larger army of volunteers.
Of course, this would be the end of the IT world, for Python ruins one for programming in much of anything else.
.... yes, I only smoke the good stuff.

What is a Perl?
Eric Pederson
::::::::::::::: ::::::::::::::: :::::
domainNot="@som ething.com"
domainIs=domain Not.replace("s" ,"z")
ePrefix="".join ([chr(ord(x)+1) for x in "do"])
mailMeAt=ePrefi x+domainIs
::::::::::::::: ::::::::::::::: :::::

Jul 18 '05 #16

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

Similar topics

46
4266
by: Scott Chapman | last post by:
There seems to be an inconsistency here: Python 2.3.2 (#1, Oct 3 2003, 19:04:58) on linux2 >>> 1 == True True >>> 3 == True False >>> if 1: print "true" ....
7
3673
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc. And i have things to offer, and to request. And a lot of ideas, but who needs them.... here's an example (from type_struct.py):
17
1679
by: Jan Danielsson | last post by:
Hello all, I recently started using Python, and I must say I like it. Both the language and libraries available for it. Background: I have written an application which I use to keep track of my personal economy. I wrote it in Java because I wanted to learn the language for a course in programming at my university. Now that I have acquired an interrest in Python I was thinking about porting my program to Python.
14
4345
by: Mark Dufour | last post by:
After nine months of hard work, I am proud to introduce my baby to the world: an experimental Python-to-C++ compiler. It can convert many Python programs into optimized C++ code, without any user intervention such as adding type declarations. It uses rather advanced static type inference techniques to deduce type information by itself. In addition, it determines whether deduced types may be parameterized, and if so, it generates...
10
1552
by: James Thiele | last post by:
I noticed in PEP 3000 that print will become a function. The PEP references a thread where Guido explains this decision. The thread does not specify what the function will return. Has this been decided?
206
8377
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of the computer language Python it is claimed: \There should be one|and preferably only one|obvious...
26
1862
by: Frank Samuelson | last post by:
I love Python, and it is one of my 2 favorite languages. I would suggest that Python steal some aspects of the S language. ------------------------------------------------------- 1. Currently in Python def foo(x,y): ... assigns the name foo to a function object. Is this pythonic? Why not use the = operator like most other assignments?
45
1888
by: azrael | last post by:
Hy guys, A friend of mine i a proud PERL developer which always keeps making jokes on python's cost. Please give me any arguments to cut him down about his commnets like :"keep programing i python. maybe, one day, you will be able to program in VisualBasic" This hurts. Please give me informations about realy famous aplications.
0
9672
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
10214
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
10164
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
10001
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
9042
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
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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.