Re: Using __new__

2024-02-18 Thread Mats Wichmann via Python-list
On 2/17/24 19:24, dn via Python-list wrote: On 18/02/24 13:21, Jonathan Gossage wrote: - perhaps someone knows a better/proper way to do this? Suggested research: custom classes, ABCs, and meta-classes... Cure the old "what do you want to accomplish" question. If it's to channel access to

Re: Using __new__

2024-02-17 Thread dn via Python-list
ject class __new__ method should not choke on arguments, just ignore them. All true. So, what you're looking for is one mechanism to rule them all? Not going to happen: for exactly the reasons you've stated. If you really want to get right 'down into the weeds' with a __new__()

Re: Using __new__

2024-02-17 Thread dn via Python-list
different arguments should return the initial singleton and ignore further attempts to create a second instance. 1 "object" don't understand. Perhaps give us a broader description of the problem? Remember also ABCs (Abstract Base Classes). 2 arguments yes, must accommodate argu

Re: Using __new__

2024-02-17 Thread dn via Python-list
On 18/02/24 11:35, Jonathan Gossage via Python-list wrote: I am attempting to use the __new__ method in the following code: class SingletonExample(object): _instance = None def __new__(cls, **kwargs): if cls._instance is None: cls._instance = super().__new__(cls

Re: Using __new__

2024-02-17 Thread MRAB via Python-list
On 2024-02-17 22:35, Jonathan Gossage via Python-list wrote: I am attempting to use the __new__ method in the following code: class SingletonExample(object): _instance = None def __new__(cls, **kwargs): if cls._instance is None: cls._instance = super().__new__

Using __new__

2024-02-17 Thread Jonathan Gossage via Python-list
I am attempting to use the __new__ method in the following code: class SingletonExample(object): _instance = None def __new__(cls, **kwargs): if cls._instance is None: cls._instance = super().__new__(cls, **kwargs) return cls._instance def __init__(self

RE: Single DB connection during class's lifetime. Metaclass,singletonand __new__() examples and references.

2018-10-15 Thread Ryan Johnson
Thank you. That clears everything up. Sent from Mail for Windows 10 From: Cameron Simpson Sent: Saturday, October 13, 2018 6:06 PM To: Ryan Johnson Cc: python-list@python.org Subject: Re: Single DB connection during class's lifetime. Metaclass,singletonand __new__() examples and references

Re: Single DB connection during class's lifetime. Metaclass,singleton and __new__() examples and references.

2018-10-13 Thread Cameron Simpson
On 12Oct2018 13:28, Ryan Johnson wrote: Thanks for the clarification. If I am creating a class variable, are you suggesting I perform the “if it exists, great, otherwise make it” logic in the __init__ block or in the class definition block? Will that even run in a class definition? The clas

RE: Single DB connection during class's lifetime. Metaclass,singleton and __new__() examples and references.

2018-10-12 Thread Ryan Johnson
AM To: python-list@python.org Subject: Re: Single DB connection during class's lifetime. Metaclass,singleton and __new__() examples and references. On 12/10/2018 01:19, Ryan Johnson wrote: > I am working on using mysql.connector in a class and have found an example of > how to crea

Re: Single DB connection during class's lifetime. Metaclass, singleton and __new__() examples and references.

2018-10-12 Thread Thomas Jollans
You can have multiple cursors on the same connection. Also, within the code example, and in Python in general, why does there needs to be a __new__ constructor when there’s an __init__ constructor? I’ve read about the usage of singletons. It seems you could create singletons within __init__ o

Re: Single DB connection during class's lifetime. Metaclass, singleton and __new__() examples and references.

2018-10-12 Thread dieter
(i.e. to convince you that several functions instantiating "postgres" indeed share a common postgres connection. In your own productive class, you would likely not have "print" statements. > Also, within the code example, and in Python in general, why does there needs >

Single DB connection during class's lifetime. Metaclass, singleton and __new__() examples and references.

2018-10-11 Thread Ryan Johnson
in general, why does there needs to be a __new__ constructor when there’s an __init__ constructor? I’ve read about the usage of singletons. It seems you could create singletons within __init__ or __new__ . Any enlightenment would be really helpful. I am very sleep-deprived, so I’m sorry if th

Re: Why do we nned both - __init__() and __new__()

2017-09-08 Thread Gregory Ewing
dieter wrote: In the general case, constructing an object can be split into two subtasks: obtain a raw piece of storage able to manage the object's state; initialize the object's state. The first subtask is handled by "__new__", the second by "__init__". E

Re: Why do we nned both - __init__() and __new__()

2017-09-08 Thread dieter
Andrej Viktorovich writes: > For my understanding both - __init__() and __new__() works like constructors. > And __new__() looks is closer to constructor. __init__() is more for variable > initialization. Why I can't just initialize in __init__() ? > > class ExampleClas

Re: Why do we nned both - __init__() and __new__()

2017-09-07 Thread Ben Finney
Andrej Viktorovich writes: > For my understanding both - __init__() and __new__() works like > constructors. Not true, they work quite differently and have very different jobs. > And __new__() looks is closer to constructor. __init__() is more for > variable initialization. That&#

Re: Why do we nned both - __init__() and __new__()

2017-09-07 Thread Steve D'Aprano
On Thu, 7 Sep 2017 08:57 pm, Andrej Viktorovich wrote: > Hello > > For my understanding both - __init__() and __new__() works like constructors. > And __new__() looks is closer to constructor. __init__() is more for variable > initialization. Why I can't just initialize in _

Re: Why do we nned both - __init__() and __new__()

2017-09-07 Thread Rustom Mody
On Thursday, September 7, 2017 at 4:27:48 PM UTC+5:30, Andrej Viktorovich wrote: > Hello > > For my understanding both - __init__() and __new__() works like constructors. > And __new__() looks is closer to constructor. __init__() is more for variable > initialization. W

Why do we nned both - __init__() and __new__()

2017-09-07 Thread Andrej Viktorovich
Hello For my understanding both - __init__() and __new__() works like constructors. And __new__() looks is closer to constructor. __init__() is more for variable initialization. Why I can't just initialize in __init__() ? class ExampleClass(object): def __new__(cls,value):

Re: __new__ and __init__ - why does this work?

2017-08-09 Thread Ethan Furman
def __new__(cls, *args, **kwargs): self = super(UniqueSet, cls).__new__(cls, *args, **kwargs) self._initialized = False return UniqueSet._registry.setdefault(self, self) def __init__(self, *args, **kwargs): if not self._initialized:

Re: __new__ and __init__ - why does this work?

2017-08-09 Thread Ian Kelly
On Aug 9, 2017 4:39 PM, "Ian Kelly" wrote: By the way, "whom" is not the verb object in "Whom do you think is going to call", so it should properly be "who do you think is going to call". I point this out because although I don't really care when people use "who" incorrectly, it looks pretty si

Re: __new__ and __init__ - why does this work?

2017-08-09 Thread Ian Kelly
On Wed, Aug 9, 2017 at 2:20 PM, Ethan Furman wrote: > On 08/09/2017 12:59 PM, Ian Pilcher wrote: > >> I do want to prevent frozenset.__init__ from being called *again* when >> an existing instance is returned, so I've decided to take this >> approach: >> >&

Re: __new__ and __init__ - why does this work?

2017-08-09 Thread Ethan Furman
On 08/09/2017 12:59 PM, Ian Pilcher wrote: I do want to prevent frozenset.__init__ from being called *again* when an existing instance is returned, so I've decided to take this approach: def __new__(cls, *args, **kwargs): self = super(UniqueSet, cls).__new__(cls, *args, **k

Re: __new__ and __init__ - why does this work?

2017-08-09 Thread Ian Pilcher
does something (phones home to Guido?) so you shouldn't block it if you don't need to. I do want to prevent frozenset.__init__ from being called *again* when an existing instance is returned, so I've decided to take this approach: def __new__(cls, *args, **kwargs): se

Re: __new__ and __init__ - why does this work?

2017-08-09 Thread Ian Pilcher
On 08/08/2017 10:19 PM, Ian Kelly wrote: It's initialized by the superclass call to __new__. frozenset is immutable, and __init__ methods of immutable types generally don't do anything (if they could, then they wouldn't be immutable), which is why it doesn't really matter tha

Re: __new__ and __init__ - why does this work?

2017-08-09 Thread Steve D'Aprano
On Thu, 10 Aug 2017 12:39 am, Dennis Lee Bieber wrote: > On Wed, 09 Aug 2017 22:54:00 +1000, Steve D'Aprano > declaimed the following: > >>Its not just frozenset. Any mutable class must initialise its instances in >>__new__. Immutable classes can use either __

Re: __new__ and __init__ - why does this work?

2017-08-09 Thread Steve D'Aprano
ty and value *wink* I doubt very much you're actually saving any time, since you create a temporary frozen set before returning the one in the cache. You might save some memory though. >class UniqueSet(frozenset): >_registry = dict() >def __new__(cls, *a

Re: __new__ and __init__ - why does this work?

2017-08-09 Thread Peter Otten
Ian Pilcher wrote: > I have created a class to provide a "hash consing"[1] set. > >class UniqueSet(frozenset): > >_registry = dict() > >def __new__(cls, *args, **kwargs): >set = frozenset(*args, **kwargs) >t

Re: __new__ and __init__ - why does this work?

2017-08-08 Thread Ian Kelly
On Tue, Aug 8, 2017 at 6:08 PM, Ian Pilcher wrote: > I have created a class to provide a "hash consing"[1] set. > > class UniqueSet(frozenset): > > _registry = dict() > > def __new__(cls, *args, **kwargs): > set = frozense

__new__ and __init__ - why does this work?

2017-08-08 Thread Ian Pilcher
I have created a class to provide a "hash consing"[1] set. class UniqueSet(frozenset): _registry = dict() def __new__(cls, *args, **kwargs): set = frozenset(*args, **kwargs) try: return UniqueSet._registry[set] excep

Round-Trip Object Correspondence, Weakrefs And __new__

2016-08-09 Thread Lawrence D’Oliveiro
ook up the result in this dictionary, and return the corresponding object. Entries in this dictionary would automatically be made every time a new object was created. To manage this, the object must define a __new__ method <https://docs.python.org/3/reference/datamodel.html#object.__new__> t

Re: What arguments are passed to the __new__ method ?

2016-03-02 Thread Ian Kelly
On Wed, Mar 2, 2016 at 10:57 AM, Rob Gaddi wrote: > Peter Pearson wrote: > >> On Tue, 1 Mar 2016 18:24:12 +0100, ast wrote: >>> >>> It's not clear to me what arguments are passed to the >>> __new__ method. Here is a piece of code: >>> >&

Re: What arguments are passed to the __new__ method ?

2016-03-02 Thread Rob Gaddi
Peter Pearson wrote: > On Tue, 1 Mar 2016 18:24:12 +0100, ast wrote: >> >> It's not clear to me what arguments are passed to the >> __new__ method. Here is a piece of code: >> >> >> class Premiere: >> >> def __new__(cls, price): >&

Re: What arguments are passed to the __new__ method ?

2016-03-02 Thread Peter Pearson
On Tue, 1 Mar 2016 18:24:12 +0100, ast wrote: > > It's not clear to me what arguments are passed to the > __new__ method. Here is a piece of code: > > > class Premiere: > > def __new__(cls, price): > return object.__new__(cls) > > def __i

Re: What arguments are passed to the __new__ method ?

2016-03-02 Thread Steven D'Aprano
o understand this. > then _call__ launches __new__ and then __init__ > This would be coherent with the language > > or is it __new__ then __init__ as said on courses I read ? Let us create a metaclass that inherits from `type`: class Meta(type): def __new__(meta, name, bases, n

Re: What arguments are passed to the __new__ method ?

2016-03-02 Thread ast
MyClass if overriden) ? then _call__ launches __new__ and then __init__ This would be coherent with the language or is it __new__ then __init__ as said on courses I read ? -- https://mail.python.org/mailman/listinfo/python-list

Re: What arguments are passed to the __new__ method ?

2016-03-01 Thread eryk sun
herits object.__call__. That function, or > the implementation of the CALL FUNCTION bytecode, must notice that > Premiere.__new__ is object.__new__, by inheritance, and only pass Premiere > and not 1000. It's not handled in bytecode or __call__. In CPython the type_call function passes its ar

Re: What arguments are passed to the __new__ method ?

2016-03-01 Thread Terry Reedy
On 3/1/2016 12:24 PM, ast wrote: Hello It's not clear to me what arguments are passed to the __new__ method. The objects passed to any function are the objects that are passed. The type and number of objects that *should be* passed depends on the signature of the function. If cl

Re: What arguments are passed to the __new__ method ?

2016-03-01 Thread eryk sun
On Tue, Mar 1, 2016 at 11:24 AM, ast wrote: > > class Premiere: > > def __new__(cls, price): > return object.__new__(cls, price) > >def __init__(self, price): >pass > > p = Premiere(1000) > > it fails. It is strange because according

What arguments are passed to the __new__ method ?

2016-03-01 Thread ast
Hello It's not clear to me what arguments are passed to the __new__ method. Here is a piece of code: class Premiere: def __new__(cls, price): return object.__new__(cls) def __init__(self, price): pass p = Premiere(1000) No errors, so it seems that 2 argument

Re: Foo.__new__ is what species of method?

2015-07-13 Thread Stefan Behnel
Steven D'Aprano schrieb am 14.07.2015 um 06:54: > On Tuesday 14 July 2015 14:45, Ben Finney wrote: >> The Python reference says of a class ‘__new__’ method:: >> >> object.__new__(cls[, ...]) >> >> Called to create a new instance of class cls. __new_

Re: Foo.__new__ is what species of method?

2015-07-13 Thread Ben Finney
Steven D'Aprano writes: > py> class Spam(object): > ... def __new__(cls): > ... print cls > ... > py> Spam.__new__() # implicit first arg? > Traceback (most recent call last): > File "", line 1, in > TypeError: __new__() takes exa

Re: Foo.__new__ is what species of method?

2015-07-13 Thread Steven D'Aprano
On Tuesday 14 July 2015 14:45, Ben Finney wrote: > Howdy all, > > The Python reference says of a class ‘__new__’ method:: > > object.__new__(cls[, ...]) > > Called to create a new instance of class cls. __new__() is a static > method (special-cased so yo

Foo.__new__ is what species of method?

2015-07-13 Thread Ben Finney
Howdy all, The Python reference says of a class ‘__new__’ method:: object.__new__(cls[, ...]) Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as

Re: __new__() does not return anything, on singletong pattern

2015-03-12 Thread Mario Figueiredo
On Thu, 12 Mar 2015 22:04:30 +1100, Steven D'Aprano wrote: > >3-4 seconds to instantiate is a bit worrying, but you should look at >improving the efficiency of loading a map rather than insisting that there >should be only one map instance. Particularly in the map editor, what if >the user wants

Re: __new__() does not return anything, on singletong pattern

2015-03-12 Thread Mario Figueiredo
On Thu, 12 Mar 2015 21:41:16 +1300, Gregory Ewing wrote: >Mario Figueiredo wrote: >> But PyCharm flags the assignment >> with a warning telling me that generate() does not return anything and >> the I lose code completion on the mmap variable. > >My guess is that there is a syntax error somewhere

Re: __new__() does not return anything, on singletong pattern

2015-03-12 Thread Mario Figueiredo
On Thu, 12 Mar 2015 22:29:24 +1100, Steven D'Aprano wrote: > >I would have a loadfile() method which takes a filename on disk, opens the >file and passes the contents (or the open file object) to another method, >load() to do the actual work: > > >class Map: >d

Re: __new__() does not return anything, on singletong pattern

2015-03-12 Thread Steven D'Aprano
n existing map, one with no features added. I would have a loadfile() method which takes a filename on disk, opens the file and passes the contents (or the open file object) to another method, load() to do the actual work: class Map: def __new__(cls, width, height, fill, trea

Re: __new__() does not return anything, on singletong pattern

2015-03-12 Thread Steven D'Aprano
Mario Figueiredo wrote: > It's just a cheap global, since is ubiquitous throughout the entire > application, does behave like a singleton, and is a bit too expensive > to create. A full map in the main application takes 3 or 4 seconds to > instantiate and occupies around 2 Mb of memory. 2MB is no

Re: __new__() does not return anything, on singletong pattern

2015-03-12 Thread Mario Figueiredo
On Thu, 12 Mar 2015 21:40:03 +1300, Gregory Ewing wrote: >Mario Figueiredo wrote: > >> A different application, a map editor, needs to also instantiate an >> object of the class Map. But in this case the map needs to either be >> empty (if the user wants to create a new map), or loaded from the >

Re: __new__() does not return anything, on singletong pattern

2015-03-12 Thread Mario Figueiredo
On Thu, 12 Mar 2015 21:38:00 +1300, Gregory Ewing wrote: > >I would just provide a function: > >_map = None > >def get_map(): >global _map >if _map is None: > _map = Map() >return _map > >and document the fact that you shouldn't call Map() >directly. Oh, you are so right! Been

Re: __new__() does not return anything, on singletong pattern

2015-03-12 Thread Gregory Ewing
Mario Figueiredo wrote: But PyCharm flags the assignment with a warning telling me that generate() does not return anything and the I lose code completion on the mmap variable. My guess is that there is a syntax error somewhere in your code that's confusing the IDE. -- Greg -- https://mail.pyt

Re: __new__() does not return anything, on singletong pattern

2015-03-12 Thread Gregory Ewing
Mario Figueiredo wrote: A different application, a map editor, needs to also instantiate an object of the class Map. But in this case the map needs to either be empty (if the user wants to create a new map), or loaded from the saved map file (if the user wants to edit an existing map). Then yo

Re: __new__() does not return anything, on singletong pattern

2015-03-12 Thread Gregory Ewing
Mario Figueiredo wrote: It's just a cheap global, since is ubiquitous throughout the entire application, does behave like a singleton, and is a bit too expensive to create. A full map in the main application takes 3 or 4 seconds to instantiate and occupies around 2 Mb of memory. There's nothin

Re: __new__() does not return anything, on singletong pattern

2015-03-12 Thread Mario Figueiredo
On Wed, 11 Mar 2015 16:47:32 -0700, Ethan Furman wrote: > >You're code is good. Thanks for taking a weight off my shoulder. > > The only question is if you /really/ need a singleton -- and only > you can answer that (although plenty of folks will tell you you > don't ;) . Yeah. I debated that

Re: __new__() does not return anything, on singletong pattern

2015-03-12 Thread Mario Figueiredo
height, fill=terrain[6]): >> if Map._instance is None: >> Map._instance = super(Map, cls).__new__(cls) >> else: >> raise Exception('Cannot generate an instance of Map.') >> >> Map._instance.author =

Re: __new__() does not return anything, on singletong pattern

2015-03-11 Thread Steven D'Aprano
ass Map: > _instance = None > def __new__(cls): > if Map._instance is None: > Map._instance = super(Map, cls).__new__(cls) > return Map._instance In Python 2, you need to inherit from object for __new__ to be called. In Python 3, it doesn't matter

Re: __new__() does not return anything, on singletong pattern

2015-03-11 Thread Ethan Furman
On 03/11/2015 04:33 PM, Mario Figueiredo wrote: > The following code runs just fine. But PyCharm flags the assignment > with a warning telling me that generate() does not return anything and > the I lose code completion on the mmap variable. > > if __name__ == '__main__': > mmap = Map.generat

__new__() does not return anything, on singletong pattern

2015-03-11 Thread Mario Figueiredo
I'm fairly new to Python, so I don't know if the following is me abusing the programming language idioms, or simply a mistake of my IDE code inspection routine. I have a singleton Map class which is defined like so: class Map: _instance = None def __new__(cls):

Re: Why has __new__ been implemented as a static method?

2014-05-04 Thread Ian Kelly
On Sun, May 4, 2014 at 8:16 AM, Steven D'Aprano wrote: > On Sun, 04 May 2014 20:03:35 +1200, Gregory Ewing wrote: > >> Steven D'Aprano wrote: >>> If it were a class method, you would call it by MyBaseClass.__new__() >>> rather than explicitly providing the

Re: Why has __new__ been implemented as a static method?

2014-05-04 Thread Rotwang
On 04/05/2014 15:16, Steven D'Aprano wrote: On Sun, 04 May 2014 20:03:35 +1200, Gregory Ewing wrote: Steven D'Aprano wrote: If it were a class method, you would call it by MyBaseClass.__new__() rather than explicitly providing the cls argument. But that wouldn't be any go

Re: Why has __new__ been implemented as a static method?

2014-05-04 Thread Steven D'Aprano
On Sun, 04 May 2014 20:03:35 +1200, Gregory Ewing wrote: > Steven D'Aprano wrote: >> If it were a class method, you would call it by MyBaseClass.__new__() >> rather than explicitly providing the cls argument. > > But that wouldn't be any good, because the base

Re: Why has __new__ been implemented as a static method?

2014-05-04 Thread Gregory Ewing
Steven D'Aprano wrote: If it were a class method, you would call it by MyBaseClass.__new__() rather than explicitly providing the cls argument. But that wouldn't be any good, because the base __new__ needs to receive the actual class being instantiated, not the class that the __ne

Re: Why has __new__ been implemented as a static method?

2014-05-03 Thread Steven D'Aprano
On Sun, 04 May 2014 11:21:53 +1200, Gregory Ewing wrote: > Steven D'Aprano wrote: >> I'm not entirely sure what he means by "upcalls", but I believe it >> means to call the method further up (that is, closer to the base) of >> the inheritance tree. > &

Re: Why has __new__ been implemented as a static method?

2014-05-03 Thread Gregory Ewing
Steven D'Aprano wrote: I'm not entirely sure what he means by "upcalls", but I believe it means to call the method further up (that is, closer to the base) of the inheritance tree. I think it means this: def __new__(cls): MyBaseClass.__new__(cls) which wouldn

Re: Why has __new__ been implemented as a static method?

2014-05-03 Thread Terry Reedy
On 5/3/2014 6:37 AM, Jurko Gospodnetić wrote: Hi all. I was wandering why Python implements its __new__ method as a static and not a class method? For a technical internal reason that Guido and maybe others have explained on pydev (more than once). I forget the details partly because

Re: Why has __new__ been implemented as a static method?

2014-05-03 Thread Steven D'Aprano
On Sat, 03 May 2014 12:37:24 +0200, Jurko Gospodnetić wrote: > Hi all. > >I was wandering why Python implements its __new__ method as a static > and not a class method? Have you read Guido's tutorial on it? https://www.python.org/download/releases/2.2.3/descrintro [quote]

Why has __new__ been implemented as a static method?

2014-05-03 Thread Jurko Gospodnetić
Hi all. I was wandering why Python implements its __new__ method as a static and not a class method? __new__ always accepts a cls parameter, which lead me to believe it was a class method. Also, implementing __new__ as a class method seems natural when thinking about __new__ as &#

Re: when to use __new__, when to use __init__

2013-10-14 Thread Ethan Furman
On 10/14/2013 03:07 PM, Peter Cacioppi wrote: I've dome some reading on the difference between __new__ and __init__, and never really groked it. I just followed the advice that you should almost always use __init__. Object creation in Python is a two step process: - create the o

when to use __new__, when to use __init__

2013-10-14 Thread Peter Cacioppi
I've dome some reading on the difference between __new__ and __init__, and never really groked it. I just followed the advice that you should almost always use __init__. I recently came across a task that required using __new__ and not __init__. I was a bit intimidated at first, but i

Re: The type.__call__() method manages the calls to __new__ and __init__?

2012-11-20 Thread Ian Kelly
On Tue, Nov 20, 2012 at 10:29 AM, Marco wrote: > Because when I call an instance the __call__ method is called, and because > the classes are instances of type, I thought when I call a Foo class this > imply the call type.__call__(Foo), and so this one manages the Foo.__new__ > and

The type.__call__() method manages the calls to __new__ and __init__?

2012-11-20 Thread Marco
Looking at the documentation of Py3.3: http://docs.python.org/3.3/reference/datamodel.html#object.__new__ I saw the method `__new__()` is called automatically when I create an istance. After the call to __new__(), if it returns an instance `self` then `self.__init__()` is called. Because

Re: confused about __new__

2011-12-27 Thread Ian Kelly
ating class instances by directly invoking object.__new__ unless they really, really know what they are doing. I was just pointing out that I don't think it's actually possible to prevent classes from being instantiated. > My point was that I can indeed intercept common and convenient

Re: confused about __new__

2011-12-27 Thread K Richard Pixley
v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. class Foo: ... def __new__(cls): ... raise TypeError ... def __init__(self): ... raise TypeError ... type(object.__new__(Fo

Re: confused about __new__

2011-12-27 Thread K Richard Pixley
r "license" for more information. class Foo: ... def __new__(cls): ... raise TypeError ... def __init__(self): ... raise TypeError ... type(object.__new__(Foo)) Try: class Foo(object): def __new__(cls): return cls --rich -- http://mail.python.org/mailman/listinfo/python-list

Re: confused about __new__

2011-12-27 Thread Ian Kelly
nk that's actually possible: >> >> Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit >> (Intel)] on win32 >> Type "help", "copyright", "credits" or "license" for more information. >>>>> >&

Re: confused about __new__

2011-12-27 Thread Ian Kelly
>  With a little work, you can even prevent it from ever being instantiated. I don't think that's actually possible: Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license"

Re: confused about __new__

2011-12-27 Thread Fredrik Tolf
On Tue, 27 Dec 2011, Ian Kelly wrote: On Mon, Dec 26, 2011 at 10:48 PM, Fredrik Tolf wrote: I'm also a bit confused about __new__. I'd very much appreciate it if someone could explain the following aspects of it:  * The manual (<http://docs.python.org/reference/datamodel.html&

Re: confused about __new__

2011-12-27 Thread K Richard Pixley
On 12/26/11 21:48 , Fredrik Tolf wrote: On Mon, 26 Dec 2011, K. Richard Pixley wrote: I don't understand. Can anyone explain? I'm also a bit confused about __new__. I'd very much appreciate it if someone could explain the following aspects of it: * The manual (<http

Re: confused about __new__

2011-12-26 Thread Lie Ryan
On 12/27/2011 04:48 PM, Fredrik Tolf wrote: On Mon, 26 Dec 2011, K. Richard Pixley wrote: I don't understand. Can anyone explain? I'm also a bit confused about __new__. I'd very much appreciate it if someone could explain the following aspects of it: * The manual (<http

Re: confused about __new__

2011-12-26 Thread Ian Kelly
On Mon, Dec 26, 2011 at 10:48 PM, Fredrik Tolf wrote: > I'm also a bit confused about __new__. I'd very much appreciate it if > someone could explain the following aspects of it: > >  * The manual (<http://docs.python.org/reference/datamodel.html>) says >   t

Re: confused about __new__

2011-12-26 Thread Fredrik Tolf
On Mon, 26 Dec 2011, K. Richard Pixley wrote: I don't understand. Can anyone explain? I'm also a bit confused about __new__. I'd very much appreciate it if someone could explain the following aspects of it: * The manual (<http://docs.python.org/reference/datamodel.h

Re: confused about __new__

2011-12-26 Thread K Richard Pixley
On 12/26/11 20:53 , Steven D'Aprano wrote: On Mon, 26 Dec 2011 20:28:26 -0800, K. Richard Pixley wrote: I'm confused about the following. The idea here is that the set of instances of some class are small and finite, so I'd like to create them at class creation time, then hi

Re: confused about __new__

2011-12-26 Thread Steven D'Aprano
On Mon, 26 Dec 2011 20:28:26 -0800, K. Richard Pixley wrote: > I'm confused about the following. The idea here is that the set of > instances of some class are small and finite, so I'd like to create them > at class creation time, then hijack __new__ to simply return one o

confused about __new__

2011-12-26 Thread K. Richard Pixley
I'm confused about the following. The idea here is that the set of instances of some class are small and finite, so I'd like to create them at class creation time, then hijack __new__ to simply return one of the preexisting classes instead of creating a new one each call. This see

Re: use of __new__ to permit "dynamic" completion within (any?) IDE ?

2010-12-08 Thread nn
nce) so here it is in code: > > files: > module1.py > subpackage/module2.py > > file module1.py: > > from subpackage.module2 import class2 > > class class1(object): > >     def __new__(cls, _self=None, *args, **kwargs): >         if _self:  ## we've been pa

Re: use of __new__ to permit "dynamic" completion within (any?) IDE ?

2010-12-08 Thread Jean-Michel Pichavant
quoting eclipse page: "Pydev [...] uses advanced type inference techniques to provide features such code completion and code analysis" I don't know exactly what's hidden behind this marketing stuff. Did you try to document your method with a markup language supported by Eclipse (if there is an

Re: use of __new__ to permit "dynamic" completion within (any?) IDE ?

2010-12-08 Thread gst
On 8 déc, 16:20, gst wrote: > even with the "assert(self is _self)" in the "if _self:" condition in > the new and/or  init methods of the class1 ? damn : in the init method only of course.. -- http://mail.python.org/mailman/listinfo/python-list

Re: use of __new__ to permit "dynamic" completion within (any?) IDE ?

2010-12-08 Thread gst
On 8 déc, 16:20, gst wrote: > On 8 déc, 15:51, Jean-Michel Pichavant wrote: > > Hi, > > > gst wrote: > > > nb: so this "hack" is only relevant during dev ; once the project > > > would be finished the "hack" could be removed (i.e : in class2 init I > > > would directly do : self.object1 = object1

Re: use of __new__ to permit "dynamic" completion within (any?) IDE ?

2010-12-08 Thread gst
On 8 déc, 15:51, Jean-Michel Pichavant wrote: Hi, > gst wrote: > > nb: so this "hack" is only relevant during dev ; once the project > > would be finished the "hack" could be removed (i.e : in class2 init I > > would directly do : self.object1 = object1) > > Expect some bugs then on the 'releas

Re: use of __new__ to permit "dynamic" completion within (any?) IDE ?

2010-12-08 Thread Jean-Michel Pichavant
gst wrote: greg. nb: so this "hack" is only relevant during dev ; once the project would be finished the "hack" could be removed (i.e : in class2 init I would directly do : self.object1 = object1) Expect some bugs then on the 'release' version. I'm not sure I understood everything you menti

Re: use of __new__ to permit "dynamic" completion within (any?) IDE ?

2010-12-08 Thread gst
On 8 déc, 14:09, Steve Holden wrote: > > If you'd told us which IDE you were using we might have offered better > advice, but you seem to want to keep that a secret ("my IDE" tells us > nothing). > sorry it was totally bottom of my first message : > note: I'm using Eclipse 3.5.2 with pydev so (I

Re: use of __new__ to permit "dynamic" completion within (any?) IDE ?

2010-12-08 Thread Steve Holden
On 12/8/2010 1:01 PM, gst wrote: > As I told I try to have some kind of "ultimate" autocompletion, always > working for this kind of case (I know exactly that a certain member of > a certain class' instance will always have the same type but my IDE > doesn't seem to guess/know it.. ; I guess the be

Re: use of __new__ to permit "dynamic" completion within (any?) IDE ?

2010-12-08 Thread gst
class2 comes from another module? yes. hmmm or maybe not completely.. I think it only highly depends on what the IDE can guess on an object type used inside some methods depending on the files hierarchy.. > Would your concept work if class1 and class2 were defined in the same file? I guess

Re: use of __new__ to permit "dynamic" completion within (any?) IDE ?

2010-12-08 Thread Steven D'Aprano
ory) to name classes with an initial capital letter, and instances in all lowercase. > class class1(object): > > def __new__(cls, _self=None, *args, **kwargs): > if _self: ## we've been passed an instance already > initialyzed > ## so dire

use of __new__ to permit "dynamic" completion within (any?) IDE ?

2010-12-07 Thread gst
from subpackage.module2 import class2 class class1(object): def __new__(cls, _self=None, *args, **kwargs): if _self: ## we've been passed an instance already initialyzed ## so directly return it instead of creating a new object. return _self

Re: sometype.__new__ and C subclasses

2010-05-02 Thread Gregory Ewing
James Porter wrote: Functions like numpy.zeros_like use ndarray.__new__(subtype, ...) to create new arrays based on the shape of other arrays. Maybe the real answer to this question is "NumPy is doing it wrong" Yes, I think NumPy is doing it wrong, even for subclasses written in

Re: sometype.__new__ and C subclasses

2010-05-02 Thread James Porter
On 5/2/2010 3:58 PM, Robert Kern wrote: Well, I think we can change zeros_like() and the rest to work around this issue. Can you bring it up on the numpy mailing list? def zeros_like(a): if isinstance(a, ndarray): res = numpy.empty(a.shape, a.dtype, order=a.flags.fnc) res.fill(0) res = res.view(

Re: sometype.__new__ and C subclasses

2010-05-02 Thread Carl Banks
On May 2, 1:51 pm, Robert Kern wrote: > On 2010-05-02 15:28 , Carl Banks wrote: > > > > > On May 2, 10:48 am, James Porter  wrote: > >> On 5/2/2010 4:34 AM, Carl Banks wrote: > > >>> Why don't you use mysubtype.__new__(mysubtype,...)? > > >

Re: sometype.__new__ and C subclasses

2010-05-02 Thread Robert Kern
ike(a): if isinstance(a, ndarray): res = ndarray.__new__(type(a), a.shape, a.dtype, order=a.flags.fnc) res.fill(0) return res Well, I think we can change zeros_like() and the rest to work around this issue. Can you bring it up on the numpy mailing list? def zeros_like(a): if isinstance(

Re: sometype.__new__ and C subclasses

2010-05-02 Thread Robert Kern
On 2010-05-02 15:28 , Carl Banks wrote: On May 2, 10:48 am, James Porter wrote: On 5/2/2010 4:34 AM, Carl Banks wrote: Why don't you use mysubtype.__new__(mysubtype,...)? If you wrote mysubtype in C, and defined a different tp_new than ndarray, then this exception will trigger. A

  1   2   3   >