Re: Is it possible to call a class but without a new instance created?

2018-06-19 Thread Jach Fong
Jim Lee at 2018/6/19 PM 03:44 wrote: On 06/18/2018 09:22 PM, Jach Fong wrote: Ben Finney at 2018/6/19 PM 10:20 wrote: Jach Fong writes: Although it passed the first examination, I have no idea if it can work correctly in the real application:-) Neither do I. What is the real-world

Re: Is it possible to call a class but without a new instance created?

2018-06-19 Thread Jim Lee
On 06/18/2018 09:22 PM, Jach Fong wrote: Ben Finney at 2018/6/19 PM 10:20 wrote: Jach Fong writes: Although it passed the first examination, I have no idea if it can work correctly in the real application:-) Neither do I. What is the real-world problem you are trying to solve? Why do you

Re: Is it possible to call a class but without a new instance created?

2018-06-19 Thread Jach Fong
Ben Finney at 2018/6/19 PM 10:20 wrote: Jach Fong writes: Although it passed the first examination, I have no idea if it can work correctly in the real application:-) Neither do I. What is the real-world problem you are trying to solve? Why do you think this (and not some more idiomatic

Re: Is it possible to call a class but without a new instance created?

2018-06-18 Thread Ben Finney
Jach Fong writes: > Although it passed the first examination, I have no idea if it can > work correctly in the real application:-) Neither do I. What is the real-world problem you are trying to solve? Why do you think this (and not some more idiomatic Python feature) is needed for solving that

Re: Is it possible to call a class but without a new instance created?

2018-06-18 Thread Jach Fong
Ben Finney at 2018/6/18 PM 03:29 wrote: Jach Fong writes: I also make a test of my own and it fails too. class A: ... objs = [] ... def __init__(self, exists=False): ... if exists: self = self.objs[0] The function parameters (bound here to the names ‘self’, ‘exists’)

Re: Is it possible to call a class but without a new instance created?

2018-06-18 Thread Jach Fong
It seems most of confusion comes from mixing up python object and tk widgets, and ignored that the tkinter is really a python-tk-interface. Thank you for pointing it out. Terry Reedy at 2018/6/18 PM 05:19 wrote: To answer the question of the title, which is a bit different from the question in

Re: Is it possible to call a class but without a new instance created?

2018-06-18 Thread Chris Angelico
On Mon, Jun 18, 2018 at 5:17 PM, Vincent Vande Vyvre wrote: > What you try to do is called a /singleton./ In this case, not necessarily a singleton, but returning a cached object that's the same for any given argument. Basically, interned objects. But yes, the same idea. ChrisA --

Re: Is it possible to call a class but without a new instance created?

2018-06-18 Thread Vincent Vande Vyvre
Le 18/06/18 à 06:48, Jach Fong a écrit : After looking into the \tkiniter\font.py source file, triggered by Jim's hint on my previous subject "Why an object changes its "address" between adjacent calls?", I get more confused. Below was quoted from the font.py: def

Re: Is it possible to call a class but without a new instance created?

2018-06-18 Thread Terry Reedy
To answer the question of the title, which is a bit different from the question in the text, yes. type(None)() always returns the singleton None object. (And one can write a singleton class in Python also.) bool() always returns one of False or True. int() and str() may return either a new

Re: Is it possible to call a class but without a new instance created?

2018-06-18 Thread Terry Reedy
On 6/18/2018 12:48 AM, Jach Fong wrote: After looking into the \tkiniter\font.py source file, triggered by Jim's hint on my previous subject "Why an object changes its "address" between adjacent calls?", I get more confused. Below was quoted from the font.py: def

Re: Is it possible to call a class but without a new instance created?

2018-06-18 Thread Peter Otten
Jach Fong wrote: > Is it possible to call a class but without a new instance created? Yes, this is possible in Python, by writing a custom __new__ method. An extreme example: >>> class Three: ... def __new__(*args): return 3 ... >>> a = Three() >>> b = Three() >>> a 3 >>> b 3 >>> a is b

Re: Is it possible to call a class but without a new instance created?

2018-06-18 Thread Ben Finney
Jach Fong writes: > I also make a test of my own and it fails too. > > >>> class A: > ... objs = [] > ... def __init__(self, exists=False): > ... if exists: self = self.objs[0] The function parameters (bound here to the names ‘self’, ‘exists’) are in the local function

Re: Is it possible to call a class but without a new instance created?

2018-06-18 Thread Chris Angelico
On Mon, Jun 18, 2018 at 2:48 PM, Jach Fong wrote: > After looking into the \tkiniter\font.py source file, triggered by Jim's > hint on my previous subject "Why an object changes its "address" between > adjacent calls?", I get more confused. > > Below was quoted from the font.py: >

Is it possible to call a class but without a new instance created?

2018-06-18 Thread Jach Fong
After looking into the \tkiniter\font.py source file, triggered by Jim's hint on my previous subject "Why an object changes its "address" between adjacent calls?", I get more confused. Below was quoted from the font.py: def nametofont(name): """Given the name of a tk