Emile van Sebille wrote:
Ethan Furman wrote:
> --d25._int = (1, 5)
Python considers names that start with a leading underscore as internal
or private, and that abuse is the burden of the abuser...
Is bytecodehacks still around? That was serious abuse :)
Emile
Good point. What I'm curious about, though, is the comment in the code
about making the Decimal instance immutable. I was unable to find docs
on that issue.
~Ethan~ 4 1697
Ethan Furman wrote:
Emile van Sebille wrote:
>Ethan Furman wrote:
>> --d25._int = (1, 5)
Python considers names that start with a leading underscore as internal or private, and that abuse is the burden of the abuser... Is bytecodehacks still around? That was serious abuse :)
Good point. What I'm curious about, though, is the comment in the code
about making the Decimal instance immutable. I was unable to find docs
on that issue.
There's something in the Language Reference, chapter 3.1 'Objects, Values
and Types'.
Mel.
Mel wrote:
Ethan Furman wrote:
>>Emile van Sebille wrote:
>>>Ethan Furman wrote:
--d25._int = (1, 5)
Python considers names that start with a leading underscore as internal or private, and that abuse is the burden of the abuser... Is bytecodehacks still around? That was serious abuse :)
Good point. What I'm curious about, though, is the comment in the code about making the Decimal instance immutable. I was unable to find docs on that issue.
There's something in the Language Reference, chapter 3.1 'Objects, Values
and Types'.
Mel.
Thanks, Mel.
I had actually read that once already, but your post caused me to reread
it, and evidently the ideas there had had enough time to percolate
through my mind.
--from decimal import Decimal
--d25 = Decimal(25)
--d25
Decimal("25")
--d25.testing = 'immutable'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Decimal' object has no attribute 'testing'
Decimals are immutable in that we cannot add new attributes to them.
The documentation in Language Reference 3.4.2.4 '__slots__' has this to say:
If defined in a new-style class, __slots__ reserves space for
the declared variables and prevents the automatic creation of
__dict__ and __weakref__ for each instance.
and
Without a __dict__ variable, instances cannot be assigned new
variables not listed in the __slots__ definition. Attempts to
assign to an unlisted variable name raises AttributeError.
So the question I have now is this: is __new__ necessary, or would
__init__ have also worked? Let's see...
class tester(object):
__slots__ = ['test1', 'test2', 'test3']
def __init__(self, value1, value2, value3):
self.test1 = value1
self.test2 = value2
self.test3 = value3
--import tester
--testee = tester.tester(1, 2, 3)
--testee
<tester.tester object at 0x009E7328>
--dir(testee)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
'__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__slots__', '__str__', 'test1', 'test2',
'test3']
--testee.test4 = 4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tester' object has no attribute 'test4'
For this simple test, it looks like __init__ works just fine. So,
besides consistency (which is important) am I missing any other reasons
to use __new__ instead of __init__?
~Ethan~
On Aug 4, 11:46*am, Ethan Furman <et...@stoneleaf.uswrote:
Mel wrote:
Ethan Furman wrote:
>Emile van Sebille wrote:
>>Ethan Furman wrote:
>>* *--d25._int = (1, 5)
>>Python considers names that start with a leading underscore as internal or private, and that abuse is the burden of the abuser... Is bytecodehacks still around? *That was serious abuse *:)
>Good point. *What I'm curious about, though, is the comment in the code about making the Decimal instance immutable. *I was unable to find docs on that issue.
There's something in the Language Reference, chapter 3.1 'Objects, Values
and Types'.
* * * * Mel.
Thanks, Mel.
I had actually read that once already, but your post caused me to reread
it, and evidently the ideas there had had enough time to percolate
through my mind.
--from decimal import Decimal
--d25 = Decimal(25)
--d25
Decimal("25")
--d25.testing = 'immutable'
Traceback (most recent call last):
* *File "<stdin>", line 1, in <module>
AttributeError: 'Decimal' object has no attribute 'testing'
Decimals are immutable in that we cannot add new attributes to them.
The documentation in Language Reference 3.4.2.4 '__slots__' has this to say:
* * * * If defined in a new-style class, __slots__ reserves spacefor
* * * * the declared variables and prevents the automatic creation of
* * * * __dict__ and __weakref__ for each instance.
and
* * * * Without a __dict__ variable, instances cannot be assignednew
* * * * variables not listed in the __slots__ definition. Attempts to
* * * * assign to an unlisted variable name raises AttributeError..
So the question I have now is this: *is __new__ necessary, or would
__init__ have also worked? *Let's see...
class tester(object):
* * *__slots__ = ['test1', 'test2', 'test3']
* * *def __init__(self, value1, value2, value3):
* * * * *self.test1 = value1
* * * * *self.test2 = value2
* * * * *self.test3 = value3
--import tester
--testee = tester.tester(1, 2, 3)
--testee
<tester.tester object at 0x009E7328>
--dir(testee)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
* '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__',
* '__repr__', '__setattr__', '__slots__', '__str__', 'test1', 'test2',
* 'test3']
--testee.test4 = 4
Traceback (most recent call last):
* *File "<stdin>", line 1, in <module>
AttributeError: 'tester' object has no attribute 'test4'
For this simple test, it looks like __init__ works just fine. *So,
besides consistency (which is important) am I missing any other reasons
to use __new__ instead of __init__?
If you subclass a builtin immutable like int then you need to override
__new__, as __init__ has no effect. Decimal is written in python, so
this is irrelevant, but if there are plans to rewrite it in C (which I
believe there are) then it'd need to use __new__ at that point. Using
__new__ even in the python version then becomes a way to future-proof
the API.
Rhamphoryncus wrote:
On Aug 4, 11:46 am, Ethan Furman <et...@stoneleaf.uswrote:
>>Mel wrote:
>>>Ethan Furman wrote:
>>>>Emile van Sebille wrote:
>>>>>Ethan Furman wrote:
>>>>> --d25._int = (1, 5)
>>>>>Python considers names that start with a leading underscore as internal >or private, and that abuse is the burden of the abuser... >Is bytecodehacks still around? That was serious abuse :)
>>>>Good point. What I'm curious about, though, is the comment in the code about making the Decimal instance immutable. I was unable to find docs on that issue.
>>>There's something in the Language Reference, chapter 3.1 'Objects, Values and Types'.
>> Mel.
Thanks, Mel.
I had actually read that once already, but your post caused me to reread it, and evidently the ideas there had had enough time to percolate through my mind.
--from decimal import Decimal --d25 = Decimal(25) --d25 Decimal("25") --d25.testing = 'immutable' Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Decimal' object has no attribute 'testing'
Decimals are immutable in that we cannot add new attributes to them.
The documentation in Language Reference 3.4.2.4 '__slots__' has this to say: If defined in a new-style class, __slots__ reserves space for the declared variables and prevents the automatic creation of __dict__ and __weakref__ for each instance. and Without a __dict__ variable, instances cannot be assigned new variables not listed in the __slots__ definition. Attempts to assign to an unlisted variable name raises AttributeError.
So the question I have now is this: is __new__ necessary, or would __init__ have also worked? Let's see...
class tester(object): __slots__ = ['test1', 'test2', 'test3'] def __init__(self, value1, value2, value3): self.test1 = value1 self.test2 = value2 self.test3 = value3
--import tester --testee = tester.tester(1, 2, 3) --testee <tester.tester object at 0x009E7328> --dir(testee) ['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__slots__', '__str__', 'test1', 'test2', 'test3'] --testee.test4 = 4 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'tester' object has no attribute 'test4'
For this simple test, it looks like __init__ works just fine. So, besides consistency (which is important) am I missing any other reasons to use __new__ instead of __init__?
If you subclass a builtin immutable like int then you need to override
__new__, as __init__ has no effect. Decimal is written in python, so
this is irrelevant, but if there are plans to rewrite it in C (which I
believe there are) then it'd need to use __new__ at that point. Using
__new__ even in the python version then becomes a way to future-proof
the API.
Thanks, Rhamphoryncus! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Michele Simionato |
last post by:
Let me show first how does it work for tuples:
>>> class MyTuple(tuple):
.... def __new__(cls,strng): # implicit conversion string of ints => tuple
.... return...
|
by: Felix Wiemann |
last post by:
Sometimes (but not always) the __new__ method of one of my classes
returns an *existing* instance of the class. However, when it does
that, the __init__ method of the existing instance is called...
|
by: could ildg |
last post by:
As there is already __init__, why need a __new__?
What can __new__ give us while __init__ can't?
In what situations we should use __new__?
And in what situations we must use __new__?
Can __new__...
|
by: James Stroud |
last post by:
Hello All,
I'm running 2.3.4
I was reading the documentation for classes & types
http://www.python.org/2.2.3/descrintro.html
And stumbled on this paragraph:
"""
__new__ must return an...
|
by: Ken Schutte |
last post by:
Hi,
I'm been trying to create some custom classes derived from some of
python's built-in types, like int and list, etc. I've run into some
trouble, which I could explain with a couple simple...
|
by: Paulo da Silva |
last post by:
Sorry to put here too many questions about __init__ __new__
stuff but I always found a new problem when using them.
I have searched for simple __new__ docs on how to do the
basic things but find...
|
by: Frank Benkstein |
last post by:
Hi,
the behaviour I always observed when creating instances by calling the
class A is that '__init__' is always only called when the object
returned by A.__new__ is an instance of A. This can be...
|
by: Sandra-24 |
last post by:
Ok here's the problem, I'm modifying a 3rd party library (boto) to
have more specific exceptions. I want to change S3ResponseError into
about 30 more specific errors. Preferably I want to do this...
|
by: Steven D'Aprano |
last post by:
When you call a new-style class, the __new__ method is called with the
user-supplied arguments, followed by the __init__ method with the same
arguments.
I would like to modify the arguments...
|
by: Torsten Mohr |
last post by:
Hi,
i have some questions related to new style classes, they look
quite useful but i wonder if somebody can give me an example
on constructors using __new__ and on using __init__ ?
I just see...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
| |