473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

reassign to builtin possible !?

Hi there,

I am reading Learning Python 3e from Mark Lutz and just found out that
reassigning to builtins is possible.
What is the reason, why Python allows this ? IMO this is very risky
and can lead to hard to find errors.
(see also Learning Python 3e, Chapter 16, Page 315)
>>True
True
>>False
False
>>True = 1
True
1
>>True = 0
True
0

TIA,
Berni
Jan 3 '08 #1
8 1230
Bernhard Merkle wrote:
Hi there,

I am reading Learning Python 3e from Mark Lutz and just found out that
reassigning to builtins is possible.
What is the reason, why Python allows this ? IMO this is very risky
and can lead to hard to find errors.
(see also Learning Python 3e, Chapter 16, Page 315)
>>>True
True
>>>False
False
>>>True = 1
True
1
>>>True = 0
True
0
This hal always been possible. But it's not reassigning, it's shadowing -
which is a totally different beast. Shadowing builtins is bad style, but
lokal to your context. Which can get nasty of course, if you do the above
on e.g. module level.

But you can't alter the values for True/False globally with this.

Diez
Jan 3 '08 #2
On Jan 3, 2:07 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
This hal always been possible. But it's not reassigning, it's shadowing -
which is a totally different beast. Shadowing builtins is bad style, but
lokal to your context. Which can get nasty of course, if you do the above
on e.g. module level.

But you can't alter the values for True/False globally with this.
Are you sure ? what about the following example ?
Is this also shadowing ?

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>import __builtin__
__builtin__.True = False
__builtin__.True
False
>>True
False

Berni
Jan 3 '08 #3
Bernhard Merkle wrote:
On Jan 3, 2:07 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
>This hal always been possible. But it's not reassigning, it's shadowing -
which is a totally different beast. Shadowing builtins is bad style, but
lokal to your context. Which can get nasty of course, if you do the above
on e.g. module level.

But you can't alter the values for True/False globally with this.

Are you sure ? what about the following example ?
Is this also shadowing ?

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>import __builtin__
__builtin__.True = False
__builtin__.True
False
>>>True
False
I'm not entirely sure what happens there, but that seems to only work in the
interactive prompt.

--------- test.py ------------
print True

if __builtins__.True == 10:
print "I'm reassigned globally"
--------- test.py -------------

Then, in the interpreter do:

droggisch@ganesha:/tmp$ python
Python 2.5.1 (r251:54863, Oct 5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Welcome to rlcompleter2 0.96
for nice experiences hit <tabmultiple times
>>__builtins__.True = 10
import test
10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 5, in <module>
if __builtins__.True == 10:
AttributeError: 'dict' object has no attribute 'True'
>>>

Diez
Jan 3 '08 #4
-On [20080103 14:47], Bernhard Merkle (be*************@googlemail.com) wrote:
>Are you sure ? what about the following example ?
Is this also shadowing ?
It is, as it is local to your current executing interpreter. Any other Python
process that is currently running is unaffected by your shadowing. So as Diez
says, you are not tampering with it on a persistent global level.

--
Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org/ asmodai
イェルーン ラウフãƒ*ック ヴァン デル ウェルヴェン
http://www.in-nomine.org/ | http://www.rangaku.org/
Any fool can make a rule. And every fool will mind it...
Jan 3 '08 #5
>But you can't alter the values for True/False globally with this.
>
Are you sure ? what about the following example ?
Is this also shadowing ?
>>>import __builtin__
__builtin__.True = False
__builtin__.True
False
It doesn't seem to screw things up globally
>>import __builtin__
t = __builtin__.True
__builtin__.True = False
__builtin__.False = t
True
False
>>False
True
>>1 == 1
True
>>import os
os.path.isdir('.')
True
>>#if they were globally redefined, this would be False
#you'd have to actually reference __builtin__.True
My thought would be if you do something as daft as
redefining/shadowing True and False, you get the headaches that
ensue. Fortunately, since Python is explicit, you can trace back
through the code and see where the inanity occurred.
Additionally, any scoping rules mean that programmer stupidity
can't leak too badly outside the scope of the block containing
the stupidity.

It's the old "DIHWIDT! WDDT!" ("Doctor, it hurts when I do
this!", "well don't do that!") syndrome.

-tkc


Jan 3 '08 #6
On Jan 3, 7:04 am, Bernhard Merkle <bernhard.mer...@googlemail.com>
wrote:
Hi there,

I am reading Learning Python 3e from Mark Lutz and just found out that
reassigning to builtins is possible.
What is the reason, why Python allows this ? IMO this is very risky
and can lead to hard to find errors.
I don't think it's a huge issue. In fact, I think it's a feature. For
example, it'd be extremely issue to reassign open, if you wanted to
implement a virtual file system, and you couldn't modify the module
the used open.
(see also Learning Python 3e, Chapter 16, Page 315)
>True
True
>False
False
>True = 1
True
1
>True = 0
True

0

TIA,
Berni
Jan 3 '08 #7
On Jan 3, 2008 8:05 AM, Tim Chase <py*********@tim.thechases.comwrote:
But you can't alter the values for True/False globally with this.
Are you sure ? what about the following example ?
Is this also shadowing ?
>>import __builtin__
__builtin__.True = False
__builtin__.True
False

It doesn't seem to screw things up globally
>>import __builtin__
>>t = __builtin__.True
>>__builtin__.True = False
>>__builtin__.False = t
>>True
False
>>False
True
>>1 == 1
True
>>import os
>>os.path.isdir('.')
True
>>#if they were globally redefined, this would be False
>>#you'd have to actually reference __builtin__.True

My thought would be if you do something as daft as
redefining/shadowing True and False, you get the headaches that
ensue. Fortunately, since Python is explicit, you can trace back
through the code and see where the inanity occurred.
Additionally, any scoping rules mean that programmer stupidity
can't leak too badly outside the scope of the block containing
the stupidity.

It's the old "DIHWIDT! WDDT!" ("Doctor, it hurts when I do
this!", "well don't do that!") syndrome.
In Py3k this will be a syntax error, like assigning to None is now.
Possibly also in 2.6.
Jan 3 '08 #8
On Jan 3, 8:06 pm, "Chris Mellon" <arka...@gmail.comwrote:
In Py3k this will be a syntax error, like assigning to None is now.
Possibly also in 2.6.
thanks. I feed much better with that :-)
Jan 4 '08 #9

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

Similar topics

2
by: Jean-S?bastien Bolduc | last post by:
I'm afraid this is a silly question, to which I know the answer already. But let me ask anyway... In Python, is there a way to modify a builtin type's methods? for instance, modifying...
0
by: Joachim Dahl | last post by:
I would like to inherit the builtin array class and write som extension methods in C. Other people have suggested that I use numarray for that, but all I want is a simple continuous C array of...
1
by: Stephen Ferg | last post by:
Python has a builtin class for staticmethod. Seems to me that Python should also have a builtin class for abstractmethod. Something like this... ####################################### #...
10
by: Shayne Wissler | last post by:
I've overloaded the global new operator, so that I can, detect when I've run out of memory: extern void* operator new(size_t s) { void *r = malloc(s); if (!r && s) { fprintf(stderr, "Error: No...
7
by: Shailesh Humbad | last post by:
I have a class and map like this: class MyObject { ... }; map<int, MyObject> Box; Now, I want to reassign the keys of one or more of the pairs in Box based on another map<int, int> that...
6
by: Anders K. Olsen | last post by:
Hello group I'm trying to list the users and groups who has read access to a file. I use .NET 2.0 and FileInfo.GetAccessControl().GetAccessRules(...) and then loop through the...
0
by: nejucomo | last post by:
Hi folks, Quick Synopsis: A test script demonstrates a memory leak when I use pythonic extensions of my builtin types, but if I use the builtin types themselves there is no memory leak. ...
20
by: Ari Krupnik | last post by:
scripts can add methods to the prototypes of builtin objects in JaavScript. I can assign functions to String.prototype.*, for instance. I want to add a method to Node, but when I try to execute...
3
by: Schwammkopf | last post by:
Hi ! What i want to do in C++ is : int a = new a; a = 0, a = 0; int* p = &a; while (1) // any condition instead of 1
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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...
0
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,...

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.