Re: Python3 - How do I import a class from another file

2019-12-16 Thread DL Neil via Python-list
Wow, I turned my back to attend to $job and this conversation has 
'exploded' (>80 msgs)!


TLDR;
With apologies, I started to reply to your reply, but then added 'bits' 
as I read the conversation thereafter. The result (below) is a messy 
hodge-podge, for which I can only apologise (as I don't have more 
spare-time to be able to 'fix it' and edit it into some semblance of 
proper order). May I suggest a first quick-read to pick-up the 
'highlights', and then re-reading in order to make sense of the 
cross-references? I regret the appearance of circling-back between topics...




On 10/12/19 9:45 pm, R.Wieser wrote:
...


Than again, I normally camel-case my variable and function names (to make
them stand out from the build-in commands), as I normally do not use editors
with syntax highlighting.

...


:-) Always start with a descriptive* name for the variable, regardless of if
it is a compounded one or not.   As said, the type prefix is just a
reminder.


Remember that PEP-8 is (only?) applied to code submitted for inclusion 
in the Python Standard Library. It's wider application is less 
prescriptive (although many in-house style manuals take an 
embrace-and-extend approach).


Bottom-line (IMHO):
- if you are coding by yourself and only for yourself, you make your own 
'rules'!
- if you are working with others, you (all) come up with 'the rules' - 
one of which may be that you allow 'the other guy' to make his own 
rules, whilst (s)he does the same for you - and simply work-around that 
with your own (and, one assumes, v-v)
- if the organisation/team already has 'rules', then you adapt to the 
group mores...


Have you come across the bit where Guido (I think?) talks about "the 
hobgoblin" of over-applying 'rules'? To which the 'Zen of Python' adds 
(the joke) "unless you're Dutch".


The topic of naming-standards is a massive time-sink/hobgoblin (see also 
- using tabs or spaces to indent, and 'how many spaces')...
(see also: which car is 'best', which language is 'best', which team..., 
which super-hero..., do you really love me? does my bum look big in this...)


That said, there's enough to learn when taking-on a new language, 
without 'sweating the small stuff' ("cognitive load"). So, if it helps 
you to use VS Coding Standards right now, just do it. Later, as you come 
to understand more of the 'philosophies' behind Python, you will/may 
learn/adapt (see also, 'working with others', above).




*though not /too/ descriptive.   Languages which can easily have
40-character names for variables, functions and such are simply the pits,
especially when they start to differ only for a single/few character(s)
somewhere in the middle. :-(


Surely it is unlikely that all 40-characters are necessary to 
communicate meaning!? Even in Welsh?Tamil?Dutch?German?


That said, it is *so* easy with modern text editors to make universal 
changes (an error-laden task filled with so much peril it was 
strenuously-avoided in the ?good, old, days) - which ease applies as 
much to variableNMs as it does to interpreting what tab-to-indent 
means/is to mean for this module!


In other words, I play as fast-and-loose as does Python (per earlier 
comment).




rudy = Man( etc ) #rudy probably does have class, in this instance

You almost owed me a new keyboard there. :-p


Normally my lame jokes are greeted with a groan, so I'll take that as a 
compliment (regardless of how it was intended - I'm that desperate!)



Which brings me to a few points, noted throughout the conversation. 
Whereas I do offer training (not Python), hold a qualification in the 
field (specifically vocational training/andragogy), and have a research 
interest in Cognitive Psychology (yeah, yeah, blah, blah, big deal...) 
there will be very few others, similar, who are also (active) members of 
this list. Expecting high-quality 'training materials' as responses is 
probably asking 'a bit much'...



We do have folk (on the list) who are actively developing the language, 
others who use the language professionally and every-day, and still 
more, less specialised, right ?down to 'mere' hobbyists who only get to 
talk-Python at (some) weekends. Answers reflect that. However, a scan of 
(names frequently appearing in) the archives will soon reveal 'much'!


In case of misunderstanding:
There is no-one (to my knowledge) who is paid to be 'here'. All 
contributions are voluntary and provided out of people's 'free time'. It 
is not a manufacturer's 'support site'!


The best way to receive help with coding (as has been said) is to 
copy-paste *your* problematic code into a post. This allows interested 
parties to easily reproduce the problem (or not). It also ensures that 
responses are as specific as possible by providing 'focus'!


It also pays to continue the copy-paste-result pattern when replying to 
advice: 'I've tried it, and 'this' is what happened' - yet I understood 
you to say 'that' should...' Such not only ensures/contributes to 
ensuring that 

Re: Python3 - How do I import a class from another file

2019-12-15 Thread Chris Angelico
On Sun, Dec 15, 2019 at 10:01 PM  wrote:
>
> On Thu, 12 Dec 2019 11:33:25 +
> Rhodri James  wrote:
>
> > On 11/12/2019 21:32, mus...@posteo.org wrote:
> > > Plain and simple: When the refcount reaches zero.
>
> > You are assuming that "when" implies "immediately on the occurence."
>
> I'm not implying that. It's the dictionary definition of the word
> "when."
>
> > This happens to be the behaviour in CPython, but other
> > implementations vary as Chris has explained several times now.
>
> And he's right. The documentation is unclear. It should be re-worded to
> reflect that the point at which the __del__() method is called after
> the refcount reaches zero is unspecified.

In Magic: The Gathering, "when" means "after this happens, the next
time its controller has priority, and then once the ability has a
chance to resolve". In common English, "when" can often mean "at some
extremely unspecified time point after" - for instance, "it's garbage
night, so when you get home, put the bins out". Nothing in the word
"when" mandates any sort of immediacy.

Trying to stipulate all of this would make the documentation LESS
clear. The Python documentation should not read like a EULA.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-15 Thread musbur
On Thu, 12 Dec 2019 11:33:25 +
Rhodri James  wrote:

> On 11/12/2019 21:32, mus...@posteo.org wrote:
> > Plain and simple: When the refcount reaches zero.  

> You are assuming that "when" implies "immediately on the occurence."

I'm not implying that. It's the dictionary definition of the word
"when."

> This happens to be the behaviour in CPython, but other 
> implementations vary as Chris has explained several times now.

And he's right. The documentation is unclear. It should be re-worded to
reflect that the point at which the __del__() method is called after
the refcount reaches zero is unspecified. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-13 Thread Greg Ewing

On 14/12/19 5:13 pm, boB Stepp wrote:

is it true that CPython itself is free to
implement these behaviors differently in its own future versions?


Yes. That's why it's not a good idea to rely on them even
if you only intend to run your code on CPython.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-13 Thread boB Stepp
On Wed, Dec 11, 2019 at 6:51 PM Greg Ewing  wrote:
>
> On 12/12/19 3:50 am, R.Wieser wrote:
> > I was rather clear about what my used version of Python was-and-is.  I have
> > no idea why that was ignored. :-(
>
> I think we've all been talking at cross purposes for a while.
>
> What I meant to say originally was that you were relying on
> an implementation detail of current CPython, and your code
> might not work on other versions of Python. I was just
> trying to make you aware of something that is considered
> "best practice" when writing Python code, for your own
> future reference.

A question:  As these behaviors being discussed are CPython
implementation details, is it true that CPython itself is free to
implement these behaviors differently in its own future versions?



-- 
boB
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-13 Thread Antoon Pardon
On 11/12/19 14:42, R.Wieser wrote:
> Michael,
>
>> It's a two-way street.  It's hard to teach someone that won't be taught.
> Whacking a student over the head because he doesn't understand has never 
> helped the student*.

You were not whacked for not understanding. You were wacked for
pretending to
understand better than the experts here. You were telling the experts
that what
they were telling you about python couldn't be true.

>So, stop whacking and start /teaching/.   Like 
> thinking of examples that could show the student where he goes wrong.

Stop arguing and start listening.

-- 
Antoon Pardon

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-12 Thread Musbur

Hi Chris,


The most important distinction, which that note is emphasizing, is
that the "del" statement removes just one reference, and if there are
other references, then __del__ will not be called.


No argument there, that's how reference counting works, and it's clear 
from the docs. What is not clear from the documentation is not if or why 
or how but *when* the __del__ method is eventually called. The doc says: 
"when [an object's] reference count reaches zero." Initially I read that 
to mean "immediately upon the reference count hitting zero," but a 
couple paragraphs down we find this: "In particular:  __del__() can be 
invoked when arbitrary code is being executed, including from any 
arbitrary thread. [...]"


I used to believe that the __del__ method was called immediately after 
refcount = zero, and that only the memory management system's functions 
(typically called from __del__) could take their time doing their 
things. Not true, it seems: Calling __del__() can be deferred, and 
PyMem_Free() et. al. migh not immediately "do"  anything as well.


I've come to see the __del__ method as a tool to get rid of an object 
(and its resources) which can be used by the Python interpreter at its 
own discretion when and if it feels the need to do so, which may be 
anything between immediately after ob_refcnt == 0 and not at all. That 
said, I can't think of any reason to explicitly define __del__() in a 
pure Python class. Do you have an example?

--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-12 Thread Rhodri James

On 11/12/2019 21:32, mus...@posteo.org wrote:

On Tue, 10 Dec 2019 14:56:10 -0500
Dennis Lee Bieber  wrote:


It is called when the language IMPLEMENTATION decides to call
it. That time is not specified in the language description/reference
manual.

Yes it is:

"Note: del x doesn’t directly call x.__del__() — the former decrements
the reference count for x by one, and the latter is only called when
x’s reference count reaches zero."

Plain and simple: When the refcount reaches zero.
You are assuming that "when" implies "immediately on the occurence."  It 
doesn't.  This happens to be the behaviour in CPython, but other 
implementations vary as Chris has explained several times now.


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread Chris Angelico
On Thu, Dec 12, 2019 at 1:33 PM Daniel Haude  wrote:
>
> Am 10.12.2019 22:29 schrieb Chris Angelico:
>
> > And once again, you are maintaining your assumptions despite being
> > very clearly shown that they are wrong. "del instance" does not
> > directly call __del__ any more than "instance = 1" does. You HAVE been
> > shown.
>
> Much of the confusion in this thread comes from the murky distinction
> between "garbage collection" and "memory management." I wouldn't call a
> reference-counting system "garbage collection" per se.

https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)#Reference_counting

It's definitely garbage collection. The alternative to a GC is
explicitly choosing to free something, which is what usually happens
when you program in C.

> Another bone of contention seems to be the question exactly WHEN
> reference counting and/or garbage collection release an object's
> resources (give it back to memory management). I read the word "when" in
> the non-implementation-specific Python docs as "immediately upon" (quote
> below, caps by me):
>
> "Note: del x doesn’t directly call x.__del__() — the former decrements
> the reference count for x by one, and the latter is only called WHEN x’s
> reference count reaches zero."
>
> It is then up to Python's memory management (PyMem_Alloc et al in
> CPython) what to do, and when: Give it back to the OS, keep it around
> for later use, whatever.

The most important distinction, which that note is emphasizing, is
that the "del" statement removes just one reference, and if there are
other references, then __del__ will not be called. None of this is
about memory per se, but about objects; Python (the language) doesn't
say anything about memory, but CPython (the implementation) does. So,
for instance, *any* conformant Python implementation will have the
semantics that "a = Thing(); b = a; del a" will not call
Thing.__del__, because that thing still exists; but once all
references expire, a conformant implementation is allowed to hand
memory back to the OS immediately, or to manage its own memory pages
and only hand those back when they're empty, or even to just not
relinquish memory at all (granted, that would almost certainly be
suboptimal, but it's not violating Python's semantics).

We've been talking about garbage collection at the level of objects.
When is an object disposed of? In CPython, this happens when its
reference count hits zero, either naturally or when the cycle detector
breaks a cycle. In Jython, I'm not sure, and I think it's based on the
underlying Java interpreter's garbage collector. In a hypothetical but
fully-conformant Python implementation, there could be a
stop-the-world mark-and-sweep GC that runs every time a function
returns, or every 250 bytecode instructions, or something. None of
this cares about actual memory or when it gets returned to the system;
it's all about when objects get destroyed. And regardless of the GC
implementation, "del x" has the same effect as "x = None" as regards
the previous value in x: they both just remove one reference.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread Greg Ewing

On 12/12/19 2:17 pm, Python wrote:

I was very impressed back in the day when the
with statement and context manager protocol appeared in
Python. I've always wondered from what language(s) it was
borrowed from


It was partly inspired by the RAII pattern often used
in C++, but as far as I know, all the details are unique
to Python. Some other languages have a "with" statement,
but it does something completely different.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread musbur
On Tue, 10 Dec 2019 22:08:48 +0100
"R.Wieser"  wrote:

> And although you have been fighting me over when the __del__ method is
> called, it /is/ called directly as a result of an "del instance" and
> the refcount goes zero.  There is /no/ delay.(with the only
> exception is when a circular reference exists).
> 
> Hence, no "race condition" problem.

Under what circumstances would freeing memory in an unspecified order
gerenate race conditions (except when freeing an unused chunk of
memory too late would cause the system to run out of memory)?

Genuinely interested, not loooking for a fight.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread Daniel Haude

Am 10.12.2019 22:29 schrieb Chris Angelico:


And once again, you are maintaining your assumptions despite being
very clearly shown that they are wrong. "del instance" does not
directly call __del__ any more than "instance = 1" does. You HAVE been
shown.


Much of the confusion in this thread comes from the murky distinction 
between "garbage collection" and "memory management." I wouldn't call a 
reference-counting system "garbage collection" per se.


Another bone of contention seems to be the question exactly WHEN 
reference counting and/or garbage collection release an object's 
resources (give it back to memory management). I read the word "when" in 
the non-implementation-specific Python docs as "immediately upon" (quote 
below, caps by me):


"Note: del x doesn’t directly call x.__del__() — the former decrements 
the reference count for x by one, and the latter is only called WHEN x’s 
reference count reaches zero."


It is then up to Python's memory management (PyMem_Alloc et al in 
CPython) what to do, and when: Give it back to the OS, keep it around 
for later use, whatever.


--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread Python

Greg Ewing wrote:
...

You deal with it by not relying on __del__ for anything
that needs to be done promptly. There are always alternatives:
e.g. file objects have a close() method, and the "with"
statement was invented to make it easy to do these kinds
of things properly.


By the way, I was very impressed back in the day when the
with statement and context manager protocol appeared in
Python. I've always wondered from what language(s) it was
borrowed from (as most of good ideas in Python have been
borrowed, like comprehension from Haskell, and so on, which
is good).



--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread Greg Ewing

On 12/12/19 3:50 am, R.Wieser wrote:

I was rather clear about what my used version of Python was-and-is.  I have
no idea why that was ignored. :-(


I think we've all been talking at cross purposes for a while.

What I meant to say originally was that you were relying on
an implementation detail of current CPython, and your code
might not work on other versions of Python. I was just
trying to make you aware of something that is considered
"best practice" when writing Python code, for your own
future reference.

However, you seem to have interpreted it as "this may not
work in current CPython". You were quite right to argue
against that, because it's false. But it didn't become
apparent that's what you were thinking until after quite
a lot of heated discussion.

The reason the discussion became heated is that instead of
saying "I don't understand", you said "I don't believe you".
This came across as arrogant and put everyone's back up.


I have brought up that a delayed calling of the __del__
method could/would most likely cause race problems.  Do you have any idea
how thats solved in environents like Jython ?


You deal with it by not relying on __del__ for anything
that needs to be done promptly. There are always alternatives:
e.g. file objects have a close() method, and the "with"
statement was invented to make it easy to do these kinds
of things properly.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread musbur
On Tue, 10 Dec 2019 14:56:10 -0500
Dennis Lee Bieber  wrote:

> 
>   It is called when the language IMPLEMENTATION decides to call
> it. That time is not specified in the language description/reference
> manual.

Yes it is:

"Note: del x doesn’t directly call x.__del__() — the former decrements 
the reference count for x by one, and the latter is only called when
x’s reference count reaches zero."

Plain and simple: When the refcount reaches zero. A few lines down,
however, it says:

>   Any code that is based upon assuming memory reclamation takes
> place at any specific time (other than program exit) is erroneous. 

That is correct, but the decision when to reclaim memory is not made by
__del__ but by the memory management subsystem after (for instance, in
CPython) calls to PyMem_Free()

>   Some implementations do not use a reference counter -- they
> rely solely upon a periodic mark garbage collector. cf:

Correct again, but the fray in this thread is about when __del__ is
called, not when memory reclaim takes place. Two different things.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread Roel Schroeven

R.Wieser schreef op 11/12/2019 om 11:43:> MRAB,
>
>>  From the help:
> ...
>> Do not depend on immediate finalization of objects when they become
>> unreachable (so you should always close files explicitly).
>
> Do you notice there is not a single word about (the delayed action 
of) the

> __del__ method in there ?  Why than do you think its talking about it ?
Hm wait. You're not mixing up del and __del__() here, do you? Calling 
del does not directly lead to __del__() being called. See 
https://docs.python.org/3/reference/datamodel.html?highlight=__del__#object.__del__


"Note: del x doesn’t directly call x.__del__() — the former decrements 
the reference count for x by one, and the latter is only called when x’s 
reference count reaches zero."
> You know how I read that ?   As if its talking about /variables going 
outof

> scope/.   Makes sense ?Thought so.  :-)
Not only going out of scope. Deleting references using del does the same 
thing.

> Hmmm...  Maybe the __del__ method was created exactly to solve this late
> handling of going-outof-scope variables.  What do you think ?
__del__ is not directly related to variables going out of scope, just as 
it is not directly related to the del statement. Going out of scope and 
del have one and only one direct effect: the reference count goes down. 
Once the reference count is zero, the object can be garbage collected. 
That is when __del__() is called.



> And by the way: /when/ would you close those files explicitily ?
When you're done with them.

def count_lines(filename):
f = open(filename, 'r')
try:
return sum(1 for line in f)
finally:
f.close()

Or better with with, making it easier to make sure the file is properly 
closed even in the case of exceptions:


def count_lines(filename):
with open(filename, 'r') as f:
return sum(1 for line in f)


HTH

--
"Honest criticism is hard to take, particularly from a relative, a
friend, an acquaintance, or a stranger."
-- Franklin P. Jones

Roel Schroeven

--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread Musbur

Am 11.12.2019 11:22 schrieb R.Wieser:

I think I will just go out on a limb and just assume that the __del__ 
method
/will/ be called as part of a "del instance" request causing the 
reference
count to reach zero (directly or indirectly), before the next command 
is

executed [...].


That's what I take the word "when" to mean in the documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__

"Note: del x doesn’t directly call x.__del__() — the former decrements 
the reference count for x by one, and the latter is only called when x’s 
reference count reaches zero."



--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread Musbur

Am 11.12.2019 11:01 schrieb Greg Ewing:

On 11/12/19 7:47 am, R.Wieser wrote:

what happens when the reference becomes zero: is the __del__
method called directly (as I find logical), or is it only called when 
the
garbage collector actually removes the instance from memory (which 
Chris

thinks what happens) ?


In CPython, these are the same thing. As soon as the reference
count becomes zero, the __del__ method is called


Yes


*and* the object is removed from memory.


Not necessarily. Let's be more precise: The resources used by the object 
are handed back to memory management (marked as free for re-use).



In a Python implementation that doesn't use reference counts,
using "del" on the last reference to an object probably isn't
going to do anything to the object.


It does exactly the same thing: It tells the memory management subsystem 
that the instance's resources may be re-used.



--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread R.Wieser
Chris,

> Yes, we know exactly how that's solved,

Nice.

> but you specifically said you weren't interested in learning.

Try taking a few courses of "understanding what you read".  That is /not/ 
what I said.

Though it goes in par with your amadant "you're wrong" chanting, even if 
there is at least one Python implementation in which it is.   And you have 
been referred to here as being /the/ Python man, which makes it unlikely 
that you didn't know. :-(

Besides, if I would /not/ have wanted to learn, would I have put this much 
effort in trying to clarify my stance and create examples for it ?

> In fact, the solution was posted very early in this thread.

My apologies, I must have missed it.  Both that solution not having been 
relevant at that time as well as me-as-a-newbie not recognisning it for what 
it was could have played a role in it.. Can you quote at least a specific 
part of it ?That way I can perhaps find it back and read it.

> I think it's high time we all stopped feeding the troll.

And I think its high time you take up a few courses of what it means to be 
teacher to newbies.   You seem to have lost the capability to put yourself 
into their shoes.

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread Arjun pazhanikumar
You could write the name of the class variable you want to import for example 
class manager

On December 10, 2019, at 2:46 AM, Dennis Lee Bieber  
wrote:

On Mon, 9 Dec 2019 20:21:39 +0100, "R.Wieser" 
declaimed the following:


>
>And than when the instance is deleted the binding to the class is lost, and 
>as a result it is garbage-collected.  Right ?
>
Quite likely...


-- 
Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/

-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread R.Wieser
Bev,

> You acknowledge that you are new to Python, yet you claimed
> to know better than Python experts. You claim you want an answer
> and yet you purposefully ignored their response because it didn't match
> what you wanted to hear.

No, I didn't.  I just pointed out that their position simply wasn't logical. 
I have given them plenty of opportunities to prove me wrong.  They refused 
to take them.

I've even /proven/ that what they claimed was incorrect by a bit of 
testcode.   It was just thrown off the table. :-(

And no, me not being right in version X on platform Y doesn't mean that you 
can claim I'm wrong everywhere.  Thats simply disingenious.

> I'm blocking all messages from you.

Thats your choice.

But if you where agreeing with all the "you are wrong!" chanters, without 
realizing that there are Python versions in which I'm actually right, and 
that one of those versions is the very one still visible in my subjectline 
than I'm not so sure you could/would be of any help with other problems I 
might have.

In other words, you might be doing me a favour. :-)

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread Chris Angelico
On Thu, Dec 12, 2019 at 1:56 AM R.Wieser  wrote:
> A question though: I have brought up that a delayed calling of the __del__
> method could/would most likely cause race problems.  Do you have any idea
> how thats solved in environents like Jython ?

Yes, we know exactly how that's solved, but you specifically said you
weren't interested in learning. In fact, the solution was posted very
early in this thread.

I think it's high time we all stopped feeding the troll.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread R.Wieser
Greg,

> In CPython, these are the same thing. As soon as the reference
> count becomes zero, the __del__ method is called

That is what I assumed (the logical thing to do).

> *and* the object is removed from memory.

Which sounds reasonable too, especially in regard to the instances variables 
needing to be removed, which could possibly hold external references.

Than again, releasing those variables could have been done just after 
calling the __del__ method, with the releasing of the used memory and 
compacting done at a later point in time.   Multiple possibilities.

> But without reference counting, it has no way of
> *knowing* that it's the last reference until the garbage
> collector does its thing.

Blimy.  You've certainly got a point there.

> To demonstrate that with a code example, it would have to be run on a 
> different implementation of Python
> that didn't use reference counting, such as Jython.

I was rather clear about what my used version of Python was-and-is.  I have 
no idea why that was ignored. :-(

As a newbie I do not yet have much use for "it isn't true on version X on 
platform Y, so you're wrong" kind of replies.  Especially not when only the 
"you're wrong" part is posted ...

A question though: I have brought up that a delayed calling of the __del__ 
method could/would most likely cause race problems.  Do you have any idea 
how thats solved in environents like Jython ?

It looks to me that if you would want to have the (for example) file closed 
when you close the instance you would need to do quite a bit of manual 
keeping-track-of the instances.

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread Rhodri James

On 11/12/2019 14:17, Musbur wrote:

Am 10.12.2019 22:33 schrieb Paul Moore:


You do understand that the reference counting garbage collector is an
implementation detail of the CPython implementation *only*, don't you?


I don't think that's true. Here's a sentonce from near the top of the 
"gc" module documentation of Python 3:

https://docs.python.org/3/library/gc.html#module-gc

"Since the collector supplements the reference counting already used in 
Python, you can disable the collector if you are sure your program does 
not create reference cycles."


The way I read this is that Python automatically and immediately deletes 
objects once their refcount goes to zero, and the garbage collector only 
kicks in case of circular references or other obscure circumstances. The 
documentation makes no reference to the specific Python implementation, 
so I believe this is true for CPython as well as others.


That's a bug in the documentation, and needs fixing; thanks for pointing 
it out!  It is certainly true that some other Python implementations 
(Jython for example) do not use reference counting, and disabling the 
collector may have further-reaching implications for those 
implementations.  I can't speak to the details; the only other 
implementation I use is Micropython, and I don't use that often enough 
to have cared about the details of garbage collection beyond noting that 
it's different to CPython.


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread Musbur

Am 10.12.2019 22:33 schrieb Paul Moore:


You do understand that the reference counting garbage collector is an
implementation detail of the CPython implementation *only*, don't you?


I don't think that's true. Here's a sentonce from near the top of the 
"gc" module documentation of Python 3:

https://docs.python.org/3/library/gc.html#module-gc

"Since the collector supplements the reference counting already used in 
Python, you can disable the collector if you are sure your program does 
not create reference cycles."


The way I read this is that Python automatically and immediately deletes 
objects once their refcount goes to zero, and the garbage collector only 
kicks in case of circular references or other obscure circumstances. The 
documentation makes no reference to the specific Python implementation, 
so I believe this is true for CPython as well as others.


To be specific: Within the semantics of the Python documentation, 
freeing the resources used by an object by explicitly or implicitly 
using "del" is not garbage collection. Python garbage collection is like 
street cleaning in real life: If everybody looked after their own trash, 
we wouldn't need a municipal service to do it.


When I first read about the Python garbage collector I was puzzled at 
the possibility of disabling it, thinking that over time a long-running 
program would fill all memory because no object's resources would ever 
be freed. But that is clearly  not the case. Even Instagram can live 
without garbage collecton (although if you look how much garbage is on 
Instagram, maybe they should re-enable it):


https://instagram-engineering.com/dismissing-python-garbage-collection-at-instagram-4dca40b29172


The (implementation independent) language semantics makes no assertion
about what form of garbage collection is used, and under other garbage
collectors, there can be an indefinite delay between the last
reference to a value being lost and the object being collected (which
is when __del__ gets called).


Only when there are circular references. Otherwise every Python 
implementation will delete objects once their refcount goes to zero, 
wven when there is no garbage collection at all, see the doc.


There is not even a guarantee that CPython will retain the reference 
counting GC in future versions.


There is no "reference counting GC" in Python. Freeing objects based on 
their reference count going to zero happens independently of the GC, see 
the official docs quoted above.



--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread R.Wieser
Antoon,

> There is a difference between not understanding why things are as
> you are told, and assuming because you don't understand, what you
> are told is wrong.

The other side is you guys, expecting from me that, even though I 
appearantly do not understand it, just take whatever has being said as the 
truth.

> Instead of trying to understand, you started to dispute.

Correct.   I didn't understand, so I argued my point, and expected the other 
to argue his.   My arguing could have clarified where I when off my rockers, 
his arguing could have shown me where I did.   Whats wrong with that ?

> Yes, you made a mistake. You relied on a specific example where
> things turned out as you expected.

And /anyone/ was allowed to come up with an example to counter it.  Heck, 
noone even tried to explain, beyond a "there are two garbage collectors", 
why my example was wrong.

> That you think you can infer general behaviour from your limited
> experience is a mistake.

Do I have any other option ?Most noone here actually tried to even 
understand what I asked, let alone try to explain their own stance.   How am 
I to learn otherwise ?I just have to create my own experiences.

And no, just parotting someone elses word has never appealed to me. 
Sorry.I need to see it (not) working , or no deal.

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread R.Wieser
Michael,

> It's a two-way street.  It's hard to teach someone that won't be taught.

Whacking a student over the head because he doesn't understand has never 
helped the student*.   So, stop whacking and start /teaching/.   Like 
thinking of examples that could show the student where he goes wrong.

*other than learning to stop asking questions and just wing it any-which-way 
he can.   Is that what you are after ?

Regards,
Rudy Wieser

P.s.
What would that "with" example/proof look like according to you ?  As a 
newbie I can ofcourse take a swipe at it, but there is that a rather large 
chance (understatement) that he /ofcourse/ will do it wrong.



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread R.Wieser
Michael,

> You said it by implication.
>
> quote:  "And as I do not think the designers of the language where that
> stupid I must therefore reject your claim to what you think happens."

I suggest you try reading that again, and imagine it being two centences, 
the first ending after "stupid" and the second starting with the  folowing 
"I". The first is a reasoning to the rejecting in the second.   Nothing 
more, nothing less.

> Since the language does behave as Chris described (calling __del__
> does not always happen when you want or think it does

No, it doesn't (I described a testprogram that contradicted it a number of 
messages back).  And trying to play of /exceptions of the rule/ as the only 
thing that matters is simply disingenious.

But, feel free to post an example or just a link to supporting your/Chris 
claim.

> You can imply that I'm stupid all you want because I can't "shoot
> holes in your example" to your satisfaction.

Wait, what ?You actually already tried that ?When ?

But, If I can shoot holes in your shooting than who's problem is that ?  :-)

And no, I do not try to imply you are stupid, but perhaps just have to much 
of "I remember it this way, so that must be the only truth" in your mind - 
without actually knowing (anymore?) why that truth is just that.

I challenged you to think for yourself, instead of just going blind on 
someone elses say-so.  Maybe you and others are right, but you have done 
little to /explain/ that to me.

> There's no such thing as "their facts."  There are facts, lies, and
> just opinions.  What Chris said was fact.

In my book anything thats unproven is just an opinion.  Its as simple as 
that.

> Even if it isn't supported by your brief and incomplete testing.

Go ahead, shoot holes into it.Just bluntly, without any kind of 
underbuilding, claiming stuff like that doesn't work with me.  Which by now 
you should have been aware of ...

> I was quite happy to see your posts up until this point. It
> appeared that you were making great headway and maybe
> even enjoying Python

I was thinking so to.

But this morning I seriously contemplating of at least ditching this 
newsgroup, if not also Python, because of the incapability (or 
unwillingness?) of the different contributors to place themselves in the 
shoes of me, a newbie.

Yeah, it works both ways. :-(((

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread Bev In TX

> On Dec 11, 2019, at 6:09 AM, R.Wieser  wrote:
> 
> Paul,
> 
>> You do understand that the reference counting garbage collector is an
>> implementation detail of the CPython implementation *only*, don't you?
> 
> No, I don't.   But do tell how that matters to me.
> 
>> there can be an indefinite delay between the last reference to
>> a value being lost and the object being collected (which is
>> when __del__ gets called).
> 
> Nope.
> 
> As I've posted a number of messages back, I've created a test program which 
> disables that periodically-called garbage collector and than created and 
> destroyed a class instance.  The print command I had put into its __del__ 
> method was called regardless.Which, according to your above claim, 
> should not have happened.
> 
> I (currently) do not care much for how other implementations do it, but feel 
> free to find one which actually does it the way you claim it happens.   But 
> if-and-when you find one be sure to also post how they deal with the, also 
> mentioned before, inherent race conditions. :-)
> 
>> If all you are interested is the semantics of the current CPython
>> release, then your statements are true.
> 
> Thank you !
> 
> Thats, for the moment, /all/ I needed to know.  (no Dennis, I have not 
> forgotten you said the same :-) ).
> 
> I hope you do realize you are now contradicting Greg, who started all of 
> this by his blunt claim that "it was only working by accident" as well as 
> Chris who agreed with him ?
> 
>> But why would anyone here know that you were looking at the
>> situation from such a limited perspective?
> 
> Look at the subject line and my initial question.  It pretty-much /screams/ 
> that I'm a new to the language (and specifies which version).  Do you think 
> that I care much, if anything, about how /other/ implementations work before 
> I have got a handle on this one ?
> 
> But do tell: How many Python 3 (CPython?  is that the same?) implementations 
> are there, and how many do actually lazily call the __del__ method (when its 
> trying to free up some memory) ?
> 
>> And your attitude seems to be confrontational and aggressive.
> 
> I take it you mean "has become".  Yep, that happens when I ask a simple 
> question and everyone just keeps posting "your have to take our word for 
> it!" posts, while refusing to even address my "but this is what happens!" 
> examples.
> 
> Besides, I'm Dutch, I'm supposed to be rude. :-)
> 
> Regards,
> Rudy 

I agree with others who responded to you.  You acknowledge that you are new to 
Python, yet you claimed to know better than Python experts.  You claim you want 
an answer and yet you purposefully ignored their response because it didn’t 
match what you wanted to hear.  You have time to repeatedly vilify everyone 
connected with Python, but you have no time to read the well written 
documentation. 

I have no more time to spend on such a ungrateful person.  I’m blocking all 
messages from you.

Bev in TX
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread R.Wieser
MRAB,

> You don't "del" an instance, you "del" a reference, which might be in the 
> form of a name in a namespace.

Remember me, the newbie to the language ?

Did you understand what I tried to to say ?   Good.  Thats al that matters.

> When the instance is collected, whenever that happens to be, its "__del__" 
> method is called.

You're introducing the word "collected", while not giving /any/ explanation 
what it means to you(!) or which of the two(or more ?) garbage collectors 
does that "collecting".   And I'm supposed to figure out what happens 
regardless ?Really ?

Also, I have explained how that lazily calling of the __del__ method could 
easily create race condition problems.  Care to comment ?

> The language definition doesn't specify that reference counting must be 
> used; that's just what CPython happens to use, and it's nice in that 
> collection tends to happen sooner.

Why is it that you guys keep equating "collection" to "call the __del__ 
method" ?   Explain that to me that please.

I myself can easily imagine that the __del__ method is called as a result of 
the reference count going Zero, and the actual "collection" (freeing up the 
local variables and releasing the occupied memory) takes place only when the 
implemented (single?) garbage collector feels like it (though I could also 
imagine that the releasing (not collecting!) of local variables could also 
be done directly after the above __del__ method has been called).

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread R.Wieser
Paul,

> You do understand that the reference counting garbage collector is an
> implementation detail of the CPython implementation *only*, don't you?

No, I don't.   But do tell how that matters to me.

> there can be an indefinite delay between the last reference to
> a value being lost and the object being collected (which is
> when __del__ gets called).

Nope.

As I've posted a number of messages back, I've created a test program which 
disables that periodically-called garbage collector and than created and 
destroyed a class instance.  The print command I had put into its __del__ 
method was called regardless.Which, according to your above claim, 
should not have happened.

I (currently) do not care much for how other implementations do it, but feel 
free to find one which actually does it the way you claim it happens.   But 
if-and-when you find one be sure to also post how they deal with the, also 
mentioned before, inherent race conditions. :-)

> If all you are interested is the semantics of the current CPython
> release, then your statements are true.

Thank you !

Thats, for the moment, /all/ I needed to know.  (no Dennis, I have not 
forgotten you said the same :-) ).

I hope you do realize you are now contradicting Greg, who started all of 
this by his blunt claim that "it was only working by accident" as well as 
Chris who agreed with him ?

>But why would anyone here know that you were looking at the
>situation from such a limited perspective?

Look at the subject line and my initial question.  It pretty-much /screams/ 
that I'm a new to the language (and specifies which version).  Do you think 
that I care much, if anything, about how /other/ implementations work before 
I have got a handle on this one ?

But do tell: How many Python 3 (CPython?  is that the same?) implementations 
are there, and how many do actually lazily call the __del__ method (when its 
trying to free up some memory) ?

> And your attitude seems to be confrontational and aggressive.

I take it you mean "has become".  Yep, that happens when I ask a simple 
question and everyone just keeps posting "your have to take our word for 
it!" posts, while refusing to even address my "but this is what happens!" 
examples.

Besides, I'm Dutch, I'm supposed to be rude. :-)

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread R.Wieser
MRAB,

> From the help:
...
> Do not depend on immediate finalization of objects when they become 
> unreachable (so you should always close files explicitly).

Do you notice there is not a single word about (the delayed action of) the 
__del__ method in there ?  Why than do you think its talking about it ?

You know how I read that ?   As if its talking about /variables going outof 
scope/.   Makes sense ?Thought so.  :-)

Hmmm...  Maybe the __del__ method was created exactly to solve this late 
handling of going-outof-scope variables.  What do you think ?

And by the way: /when/ would you close those files explicitily ?You 
cannot just do it before any "del instance" command, as some other refence 
to the instance might still there ...

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread R.Wieser
Dennis,

This is the first time I've seen you post FUD. :-((

> It is called when the language IMPLEMENTATION decides to call it.
> That time is not specified in the language description/reference manual.

I'm rather sure it neither specifies that for any of the other commands. 
Does that mean that it could randomly delay the execution of them too 
(effectivily randomizing the program) and we should all be OK by that ?

I think I will just go out on a limb and just assume that the __del__ method 
/will/ be called as part of a "del instance" request causing the reference 
count to reach zero (directly or indirectly), before the next command is 
executed (circular references excluded - a situation which I assume happens 
by mistake, not on purpose).

Why ?   Because anything else would cause big problems in the predictability 
of the execution of a program.   And I don't think the writers/maintainers 
of the Python language where or are /that/ daft (it would kill their 
language off in no time flat).

@all
But, if anyone has an example of how that presumption will, in regular use 
(non circular references), bite me in the ass than please to post it or 
provide a link.  I do not at all mind being /proven/ wrong.

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread Greg Ewing

On 11/12/19 7:47 am, R.Wieser wrote:

what happens when the reference becomes zero: is the __del__
method called directly (as I find logical), or is it only called when the
garbage collector actually removes the instance from memory (which Chris
thinks what happens) ?


In CPython, these are the same thing. As soon as the reference
count becomes zero, the __del__ method is called *and* the object
is removed from memory.

This remains true even in the presence of reference cycles. As
long as the object is part of a cycle, it's reference count
is not zero. When the cyclic garbage collector gets around to
noticing the cycle, it breaks it, causing the reference counts
of the objects in it to become zero, in turn causing them to
be collected.

In a Python implementation that doesn't use reference counts,
using "del" on the last reference to an object probably isn't
going to do anything to the object.

You seem to think it would be logical for it to call the __del__
method there and then, instead of waiting until the object is
collected. But without reference counting, it has no way of
*knowing* that it's the last reference until the garbage
collector does its thing.

To summarise, your logic on this point is flawed.


As a simple bit of testcode has shown me (read my reply to
Chris).


We're not saying that your code won't work -- in current
CPython it will. We're saying that it relies on an
implementation detail. To demonstrate that with a code example,
it would have to be run on a different implementation of Python
that didn't use reference counting, such as Jython.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-11 Thread Antoon Pardon
On 10/12/19 22:24, R.Wieser wrote:
> Roel,
>
>> Rudy, unless/until you know the language inside out, you'd better listen 
>> to Chris and other people here who spend a lot of time and effort in 
>> helping people like you.
> I tried.   But what he said made no sense to me, and no matter how hard I 
> tried I could not get Chris (or others for that matter) to understand.   And 
> as you might have seen, I tried. In several different ways.

There is a difference between not understanding why things are as you
are told, and
assuming because you don't understand, what you are told is wrong.
Instead of trying
to understand, you started to dispute.
> The only thing I got was regurgitation, without /any/ attempt to figure out 
> where my mistake would be.  Do you have /any/ idea how frustrating that that 
> is ?   Do you ?

That is because you started to argue instead of trying to ask for
clarifications.
> Also, if I would not care about it, why would I than not simply have stopped 
> responding ?   Much easier than to try to get someone to understand.
>
> And as it turned out, I /didn't/ make a mistake.The __del__ method /is/ 
> directly called as a result of the "del instance" command (and the reference 
> count going zero).  No delay, and thus no "race condition" exist.

Yes, you made a mistake. You relied on a specific example where things
turned out as you
expected. But what you expect is not within the language specification.
If next time you
try out an other python implementation, there is no guarantee this will
still work as
you expect. That you think you can infer general behaviour from your
limited experience
is a mistake.

-- 
Antoon Pardon.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Python

Michael Torrie wrote:

On 12/10/19 2:08 PM, R.Wieser wrote:

You might know a thing or two about Python, but you (and a number of others
here) positivily stink as teachers, incapable or even unwilling to place
themselves in the shoes of a newbie.


As it happens, I've heard Chris speak about teaching Python to students.
  I suspect they learn rather well from him.  Chris is a very articulate
and great explainer.

It's a two-way street.  It's hard to teach someone that won't be taught.


+1



--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Michael Torrie
On 12/10/19 2:08 PM, R.Wieser wrote:
> You might know a thing or two about Python, but you (and a number of others
> here) positivily stink as teachers, incapable or even unwilling to place
> themselves in the shoes of a newbie.

As it happens, I've heard Chris speak about teaching Python to students.
 I suspect they learn rather well from him.  Chris is a very articulate
and great explainer.

It's a two-way street.  It's hard to teach someone that won't be taught.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread MRAB

On 2019-12-10 21:08, R.Wieser wrote:

Chris,


Once again, you make positive assertions about things you
don't understand.


And you have refused to bring any kind of help (explanation!) forward that
could have shown me the erring of my ways, as well as refusing to adress my
(attempting to explain) examples.

You might know a thing or two about Python, but you (and a number of others
here) positivily stink as teachers, incapable or even unwilling to place
themselves in the shoes of a newbie.

Only now I know, after Dennis explanation, that there are (at least) /two/
garbage collectors.  One of which is triggered by a reference count becoming
zero, and one periodically called.

And although you have been fighting me over when the __del__ method is
called, it /is/ called directly as a result of an "del instance" and the
refcount goes zero.  There is /no/ delay.(with the only exception is
when a circular reference exists).

You don't "del" an instance, you "del" a reference, which might be in 
the form of a name in a namespace.


If that was the only reference to the instance, then the instance can be 
collected.


When the instance is collected, whenever that happens to be, its 
"__del__" method is called.


CPython uses reference counting, so if del removes the only reference, 
the instance will collected the instance immediately.


The language definition doesn't specify that reference counting must be 
used; that's just what CPython happens to use, and it's nice in that 
collection tends to happen sooner.


Other implementations can use other techniques, and for some of them 
there might be a delay before collection kicks in, so if an instance is 
holding a resource, that resource would not be released as quickly.



Hence, no "race condition" problem.

But for some reason even my claiming that that would happen didn't trigger
any kind of understanding.

And you  blame /me/ for making certain (logical!) assumptions ?   Really ?


--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Michael Torrie
On 12/10/19 11:47 AM, R.Wieser wrote:
> Who did I call stupid ?I mentioned that the language doing it as Chris 
> thinks it happens would be stupid, and I gave a reason for that (race 
> conditions everywhere).  But odd: Neither him nor you nor anyone else who 
> complains about me thinking for myself has even /tried/ to shoot holes in my 
> example (or explain, let alone underbuild their own stance).  Do you have 
> /any/ idea to what that makes me think ?  Yeah, exactly that. :-((

You said it by implication.

quote:  "And as I do not think the designers of the language where that
stupid I must therefore reject your claim to what you think happens."

Since the language does behave as Chris described (calling __del__ does
not always happen when you want or think it does, hence the "with"), you
have implied that Guido and friends are stupid and that you are not.

You can imply that I'm stupid all you want because I can't "shoot holes
in your example" to your satisfaction.  Doesn't change the fact that
what we've told you are facts about Python's operation.  I'm stupid
about many things but not this one.

>> Doesn't reflect well on you, who by your own admission is still learning
>> the language.
> 
> True, I'm a newbie learning /Python/.   But it is far from the first 
> language I've used.  Experience /does/ count for something you know. :-)

In this case your experience is perhaps leading you astray when it comes
to Python.

>> And that's exactly what Chris and others did.  They explained the facts
> 
> No, they didn't.  They just TOLD me /their/ "facts".  Not an explanation 
> anywhere. :-(

There's no such thing as "their facts."  There are facts, lies, and just
opinions.  What Chris said was fact.  Even if it isn't supported by your
brief and incomplete testing.

I was quite happy to see your posts up until this point. It appeared
that you were making great headway and maybe even enjoying Python. But
now I fear you're going to end up in a lot of awesome contributors'
killfiles.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Paul Moore
On Tue, 10 Dec 2019 at 21:12, R.Wieser  wrote:
> And although you have been fighting me over when the __del__ method is
> called, it /is/ called directly as a result of an "del instance" and the
> refcount goes zero.  There is /no/ delay.(with the only exception is
> when a circular reference exists).

You do understand that the reference counting garbage collector is an
implementation detail of the CPython implementation *only*, don't you?
The (implementation independent) language semantics makes no assertion
about what form of garbage collection is used, and under other garbage
collectors, there can be an indefinite delay between the last
reference to a value being lost and the object being collected (which
is when __del__ gets called). There is not even a guarantee that
CPython will retain the reference counting GC in future versions.
Removing it would be a big change, but not impossible.

If all you are interested is the semantics of the current CPython
release, then your statements are true. But why would anyone here know
that you were looking at the situation from such a limited
perspective?

Your "logic" seems to be full of hidden assumptions and unstated
qualifications. And your attitude seems to be confrontational and
aggressive. Frankly, it's unlikely that you're going to learn much
without a change in your approach.

Paul
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Chris Angelico
On Wed, Dec 11, 2019 at 8:26 AM R.Wieser  wrote:
>
> Roel,
>
> > Rudy, unless/until you know the language inside out, you'd better listen
> > to Chris and other people here who spend a lot of time and effort in
> > helping people like you.
>
> I tried.   But what he said made no sense to me, and no matter how hard I
> tried I could not get Chris (or others for that matter) to understand.   And
> as you might have seen, I tried. In several different ways.
>
> The only thing I got was regurgitation, without /any/ attempt to figure out
> where my mistake would be.  Do you have /any/ idea how frustrating that that
> is ?   Do you ?

Given that you aren't listening when we point out where the mistake
is, yes, I know how frustrating it is.

> Also, if I would not care about it, why would I than not simply have stopped
> responding ?   Much easier than to try to get someone to understand.
>
> And as it turned out, I /didn't/ make a mistake.The __del__ method /is/
> directly called as a result of the "del instance" command (and the reference
> count going zero).  No delay, and thus no "race condition" exist.
>

And once again, you are maintaining your assumptions despite being
very clearly shown that they are wrong. "del instance" does not
directly call __del__ any more than "instance = 1" does. You HAVE been
shown.

I'm done responding to you. I'm very close to blocking you in my mail client.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread R.Wieser
Roel,

> Rudy, unless/until you know the language inside out, you'd better listen 
> to Chris and other people here who spend a lot of time and effort in 
> helping people like you.

I tried.   But what he said made no sense to me, and no matter how hard I 
tried I could not get Chris (or others for that matter) to understand.   And 
as you might have seen, I tried. In several different ways.

The only thing I got was regurgitation, without /any/ attempt to figure out 
where my mistake would be.  Do you have /any/ idea how frustrating that that 
is ?   Do you ?

Also, if I would not care about it, why would I than not simply have stopped 
responding ?   Much easier than to try to get someone to understand.

And as it turned out, I /didn't/ make a mistake.The __del__ method /is/ 
directly called as a result of the "del instance" command (and the reference 
count going zero).  No delay, and thus no "race condition" exist.

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Python3 - How do I import a class from another file

2019-12-10 Thread R.Wieser
Chris,

> Once again, you make positive assertions about things you
> don't understand.

And you have refused to bring any kind of help (explanation!) forward that
could have shown me the erring of my ways, as well as refusing to adress my
(attempting to explain) examples.

You might know a thing or two about Python, but you (and a number of others
here) positivily stink as teachers, incapable or even unwilling to place
themselves in the shoes of a newbie.

Only now I know, after Dennis explanation, that there are (at least) /two/
garbage collectors.  One of which is triggered by a reference count becoming
zero, and one periodically called.

And although you have been fighting me over when the __del__ method is
called, it /is/ called directly as a result of an "del instance" and the
refcount goes zero.  There is /no/ delay.(with the only exception is
when a circular reference exists).

Hence, no "race condition" problem.

But for some reason even my claiming that that would happen didn't trigger
any kind of understanding.

And you  blame /me/ for making certain (logical!) assumptions ?   Really ?

Regards,
Rudy Wieser



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread R.Wieser
Dennis,

> In common Python, small integers are cached internally for reuse

That is what I imagined could possibly be happening, reading the specific 
"the integer 5" formulation in the question ("reading between the lines" and 
all that).

And thanks for that explanation, underbuild an all.  :-)

> If you had an object on which you invoked "del", then it had a
> reference count, and if that reference count dropped to 0, then any
> __del__ method would be called.

That is exactly what I thought (the most logical thing), but Greg seemed to 
know better:

[quote: greg]
In that case, it was only working by accident. You were unwittingly
relying on the garbage collector to immediately clean up the object
as soon as the last reference to it disappeared.
[/quote]

My doubt to that (explicitily mentioning the __del__ method) was than picked 
up by Chris, who continued in the same vein.  After which the others started 
to pile up on me.

I hope you do realise that you are opening yourself up to those same guys ? 
After a few of those ontop of you quickly ceases to be funny. :-(

> Remember that reference counting, the main garbage collection
> mechanism in Python, can't be disabled.

Even /wanting/ to do so would be rather stupid, as it would mean the program 
would than be hemmoraging memory.

> The only garbage collection behavior you can
> alter is the generational garbage collector in the gc module.

Which I assume just takes care of compacting the available memory (and 
dealing with those (already found?) circular references).

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Rhodri James

On 10/12/2019 19:25, R.Wieser wrote:

And to repeat, you have*no*  guarantee*at all*  in the language spec that
this will happen.

There is also absolutily no guarantee in those specs that my 'puter will not
suddenly transform into a bonzai tree.   Should I now expect it to happen ?


That's an implementation detail of your computer.

--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread MRAB

On 2019-12-10 19:25, R.Wieser wrote:

Rhodri,


the object that is the integer 5 *isn't* deleted


That was not the question.  And you're now referring to the object as well
as the integer, which makes it confusing. You also have not explained what
happens instead.   In short, your above statement has ZERO value to me.

On the contrary, it's exactly the point.  You are asserting behaviour that 
you are not guaranteed.


Prove it.  I'm getting a bit fed up with you guys yapping along without
anything to back anything up.


What you disabled wasn't the garbage collector,


Prove it.  The docs.python.org page I found says something else, starting
with the pages title being "Garbage Collector".


From the help:

"""CPython implementation detail: CPython currently uses a 
reference-counting scheme with (optional) delayed detection of 
cyclically linked garbage, which collects most objects as soon as they 
become unreachable, but is not guaranteed to collect garbage containing 
circular references. See the documentation of the gc module for 
information on controlling the collection of cyclic garbage. Other 
implementations act differently and CPython may change. Do not depend on 
immediate finalization of objects when they become unreachable (so you 
should always close files explicitly)."""


Ultimately, the proof of how CPython works is in its source code, which 
is open.


And to repeat, you have *no* guarantee *at all* in the language spec that 
this will happen.


There is also absolutily no guarantee in those specs that my 'puter will not
suddenly transform into a bonzai tree.   Should I now expect it to happen ?


--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread R.Wieser
Rhodri,

> the object that is the integer 5 *isn't* deleted

That was not the question.  And you're now referring to the object as well 
as the integer, which makes it confusing. You also have not explained what 
happens instead.   In short, your above statement has ZERO value to me.

> On the contrary, it's exactly the point.  You are asserting behaviour that 
> you are not guaranteed.

Prove it.  I'm getting a bit fed up with you guys yapping along without 
anything to back anything up.

> What you disabled wasn't the garbage collector,

Prove it.  The docs.python.org page I found says something else, starting 
with the pages title being "Garbage Collector".

> And to repeat, you have *no* guarantee *at all* in the language spec that 
> this will happen.

There is also absolutily no guarantee in those specs that my 'puter will not 
suddenly transform into a bonzai tree.   Should I now expect it to happen ?

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Roel Schroeven

R.Wieser schreef op 10/12/2019 om 13:44:


Also, when you scorn something without knowing your facts,
you tend to make yourself look like a fool :)


I tend to go with logic.   No logic => it most likely doesn't work that way.

Choosing to go with a delayed action like you say will, as I already
explained, create problems later on (race conditions everywhere).  And as I
do not think the designers of the language where that stupid I must
therefore reject your claim to what you think happens.


Chris: long-time contributor to this mailing list, lots of Python 
experience, knows his stuff.

Rudy: new to Python, still learning the basics.

Chris: explains how things work, states the facts correctly.
Rudy: disregards all that and thinks he knows better.

Me: does not compute


But, as you mentioned, I do not know (all) the facts.  Please do present
them.   Maybe there is a logic in there I do not yet see.


Check out the 'with' statement.


I'm sorry, but nope.


That's a very foolish rejection.

Rudy, unless/until you know the language inside out, you'd better listen 
to Chris and other people here who spend a lot of time and effort in 
helping people like you. Don't correct them on things like this unless 
you can cite chapter and verse.

You're doing yourselves a massive disservice by acting this way.

Best regards,
Roel

--
"Honest criticism is hard to take, particularly from a relative, a
friend, an acquaintance, or a stranger."
-- Franklin P. Jones

Roel Schroeven

--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Rhodri James

On 10/12/2019 19:00, R.Wieser wrote:

MRAB,


You merely disabled the mark-and-sweep collector.


Nope.   Go check the docs on "gc"


Yep.  Go and check the docs on "gc" *carefully*

*plonk*


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Chris Angelico
On Wed, Dec 11, 2019 at 6:06 AM R.Wieser  wrote:
>
> MRAB,
>
> > You merely disabled the mark-and-sweep collector.
>
> Nope.   Go check the docs on "gc"
>

https://docs.python.org/3/library/gc.html

"""
This module provides an interface to the optional garbage collector.
... Since the collector supplements the reference counting already
used in Python, you can disable the collector if you are sure your
program does not create reference cycles.
"""

Once again, you make positive assertions about things you don't understand.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread R.Wieser
MRAB,

> You merely disabled the mark-and-sweep collector.

Nope.   Go check the docs on "gc"

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Chris Angelico
On Wed, Dec 11, 2019 at 5:51 AM R.Wieser  wrote:
>
> Michael,
>
> >> So, only the reference count gets lowered.  Yep, thats daft.
> >
> > Why?  Consider:
>
> You missed the point.
>
> It was-and-is not about decrementing a reference but still having it above
> zero, but what happens when the reference becomes zero: is the __del__
> method called directly (as I find logical), or is it only called when the
> garbage collector actually removes the instance from memory (which Chris
> thinks what happens) ?
>
> > No, that is exactly what happens.
>
> No, it doesn't.  As a simple bit of testcode has shown me (read my reply to
> Chris).

You continue to be grossly mistaken and oblivious to this fact.

> > Be careful who you call stupid.  Seriously.
>
> Who did I call stupid ?I mentioned that the language doing it as Chris
> thinks it happens would be stupid, and I gave a reason for that (race
> conditions everywhere).  But odd: Neither him nor you nor anyone else who
> complains about me thinking for myself has even /tried/ to shoot holes in my
> example (or explain, let alone underbuild their own stance).  Do you have
> /any/ idea to what that makes me think ?  Yeah, exactly that. :-((

x = []
x.append(x)
x = None

At what point does the reference count for that list drop to zero?
When should its __del__ method be called? Have you actually looked
into what you're heaping scorn onto?

> > Doesn't reflect well on you, who by your own admission is still learning
> > the language.
>
> True, I'm a newbie learning /Python/.   But it is far from the first
> language I've used.  Experience /does/ count for something you know. :-)
>
> > And that's exactly what Chris and others did.  They explained the facts
>
> No, they didn't.  They just TOLD me /their/ "facts".  Not an explanation
> anywhere. :-(
>

No, we told you facts. Facts are not owned by anyone. They simply are.

I'm pretty much done with talking to you. You aren't listening, and
you're being abrasive and scornful at every turn.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread R.Wieser
Michael,

>> So, only the reference count gets lowered.  Yep, thats daft.
>
> Why?  Consider:

You missed the point.

It was-and-is not about decrementing a reference but still having it above 
zero, but what happens when the reference becomes zero: is the __del__ 
method called directly (as I find logical), or is it only called when the 
garbage collector actually removes the instance from memory (which Chris 
thinks what happens) ?

> No, that is exactly what happens.

No, it doesn't.  As a simple bit of testcode has shown me (read my reply to 
Chris).

> Be careful who you call stupid.  Seriously.

Who did I call stupid ?I mentioned that the language doing it as Chris 
thinks it happens would be stupid, and I gave a reason for that (race 
conditions everywhere).  But odd: Neither him nor you nor anyone else who 
complains about me thinking for myself has even /tried/ to shoot holes in my 
example (or explain, let alone underbuild their own stance).  Do you have 
/any/ idea to what that makes me think ?  Yeah, exactly that. :-((

> Doesn't reflect well on you, who by your own admission is still learning
> the language.

True, I'm a newbie learning /Python/.   But it is far from the first 
language I've used.  Experience /does/ count for something you know. :-)

> And that's exactly what Chris and others did.  They explained the facts

No, they didn't.  They just TOLD me /their/ "facts".  Not an explanation 
anywhere. :-(

Regards,
Rudy Wieser 


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Rhodri James

On 10/12/2019 17:18, R.Wieser wrote:

Chris,


Okay. What should happen when you do this?

x = 5
del x

Should the integer 5 be deleted?


Yep.

What do you think happens instead ?   I've not seen you explain or support
anything in that regard, not even now.


As it happens, the object that is the integer 5 *isn't* deleted.  Once 
again, you are making assertions based on what you believe to be true, 
despite having been told otherwise.  That's fair enough, but you then 
give those assertions the weighting of fact.



There is a bit of a problem with the above though: It has got zero to do
with the __del__ I was talking about.


On the contrary, it's exactly the point.  You are asserting behaviour 
that you are not guaranteed.  It happens that current versions of 
CPython conform to your expectations.  I have a vague and unreliable 
recollection that earlier versions didn't.  Other Python interpreters 
may or may not.  The language description does not give you that 
guarantee, so relying on it is at best foolish.



 I've not seen you point out any
mistake with my example (pointing out race contition problems) either.


Greg Ewing did:

"In that case, it was only working by accident. You were unwittingly
relying on the garbage collector to immediately clean up the object
as soon as the last reference to it disappeared. That happens to be
true currently in CPython (which uses reference counting), but it's
not guaranteed by the language."

Unless you're referring to some other example.  It's a bit hard to tell 
without the context.



Not a smooth move bro.  Not a smooth move /at all/ :-(


Words fail me.


So the language designers couldn't possibly have been so stupid
as to do things this way, but you're going to ignore what they did?


Actually, they didn't.

Did you know you can disable the garbage collector ?  Well, you can.   Guess
what I saw when I disabled it, created a class instance and than deleted it
again.  Yup, the "print" command I placed in the "__del__" method did
actually show output - something that, according to you, could/should never
happen ...


What you disabled wasn't the garbage collector, it was the garbage 
collector's periodic background run to detect reference cycles and 
delete them when they have no external references.  The garbage 
collector itself ran just fine when your instance's reference count 
reached zero, exactly as everyone has been saying to you.


And to repeat, you have *no* guarantee *at all* in the language spec 
that this will happen.  The garbage collector could have waited until it 
was short of space before deleting the object, for example, which might 
not happen until your script terminates.


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread MRAB

On 2019-12-10 17:18, R.Wieser wrote:

Chris,


Okay. What should happen when you do this?

x = 5
del x

Should the integer 5 be deleted?


Yep.

What do you think happens instead ?   I've not seen you explain or support
anything in that regard, not even now.

There is a bit of a problem with the above though: It has got zero to do
with the __del__ I was talking about.  I've not seen you point out any
mistake with my example (pointing out race contition problems) either.

Not a smooth move bro.  Not a smooth move /at all/ :-(


So the language designers couldn't possibly have been so stupid
as to do things this way, but you're going to ignore what they did?


Actually, they didn't.

Did you know you can disable the garbage collector ?  Well, you can.   Guess
what I saw when I disabled it, created a class instance and than deleted it
again.  Yup, the "print" command I placed in the "__del__" method did
actually show output - something that, according to you, could/should never
happen ...

Than again, I've used Python 3 for the test.  Maybe you're remembering
something from an older version ?


You merely disabled the mark-and-sweep collector.

When the reference out reaches zero, the object is deleted, but if an 
object is part of a reference cycle, the reference count will never 
reach zero.


To deal with that, there's a mark-and-sweep collector that's called 
periodically.

--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread R.Wieser
Antoon,

> Well yes that is why it is stupid to rely on the garbage collector to do
> those things for you. That is why Greg Ewing already stated:
>
>> If your object has a method for explicitly making it release its
>> resources, it would be better to use that rather than rely on
>> the garbage collector.

I think that you are mistaken to the meaning of word "resources" in the 
above ...

>   foo1 = Bar()
>   foo2 = foo1
>   del foo1
>
> Should the object be cleaned up now or not?

Seriously ?No, as the object is still being referenced by "foo2".And 
I do not see you explain anything either. :-(

Also, what has that got to do anything with my example talking about the 
"__del__" method  I posted ?As far as I can tell ?  Nothing.

Regards,
Rudy Wieser

P.s.
Be sure to read my reply to Chris.


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread R.Wieser
Chris,

> Okay. What should happen when you do this?
>
> x = 5
> del x
>
> Should the integer 5 be deleted?

Yep.

What do you think happens instead ?   I've not seen you explain or support 
anything in that regard, not even now.

There is a bit of a problem with the above though: It has got zero to do 
with the __del__ I was talking about.  I've not seen you point out any 
mistake with my example (pointing out race contition problems) either.

Not a smooth move bro.  Not a smooth move /at all/ :-(

> So the language designers couldn't possibly have been so stupid
> as to do things this way, but you're going to ignore what they did?

Actually, they didn't.

Did you know you can disable the garbage collector ?  Well, you can.   Guess 
what I saw when I disabled it, created a class instance and than deleted it 
again.  Yup, the "print" command I placed in the "__del__" method did 
actually show output - something that, according to you, could/should never 
happen ...

Than again, I've used Python 3 for the test.  Maybe you're remembering 
something from an older version ?

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread R.Wieser
Python,

> which contradicts your previous statement.

I don't think so.

> "with" is "more stuff" it is the right way to deal with disposable 
> ressource in the first place.

Than you have missed the point altogether.  It isn't about what you think is 
the perfect way to deal with something, but how an instanciated class 
responds to an explicit "del" command.

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Michael Torrie
On 12/10/19 5:44 AM, R.Wieser wrote:
>> Well, that's exactly what happens.
> 
> So, only the reference count gets lowered.  Yep, thats daft.

Why?  Consider:

a = someobject
b = a

How many references for someobject?  After "del a" what should be be?
It's still someobject (the same object).

Or the example Antoon provided:

def foo()
   b = Bar()
   return b
   # b reference is deleted automatically


> ... unless you explain why it can only be done that way.

That is how a ref-counting resource management system works.

> I tend to go with logic.   No logic => it most likely doesn't work that way.

Interestingly I just saw a podcast recently titled, "Why too much logic
leads to irrationality."  In this case your logic is faulty because, as
you say, you lack information, which others have tried to provide.

> Choosing to go with a delayed action like you say will, as I already 
> explained, create problems later on (race conditions everywhere).  And as I 
> do not think the designers of the language where that stupid I must 
> therefore reject your claim to what you think happens.

No, that is exactly what happens.  And for good reasons.  Google for
reference-counting garbage collection.  This is not just some brand new
Python thing.  Be careful who you call stupid.  Seriously.  Doesn't
reflect well on you, who by your own admission is still learning the
language.  Python is not C++; don't code like it is.  If you need
semantics that are like RAII, then as Chris has said, you need to look
at the "with" statement and context handlers.


> But, as you mentioned, I do not know (all) the facts.  Please do present 
>them.   Maybe there is a logic in there I do not yet see.

And that's exactly what Chris and others did.  They explained the facts
and how relying on "del" to release the GPIO pin only works because you
got lucky.

>> Check out the 'with' statement.
> 
> I'm sorry, but nope.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Antoon Pardon
On 10/12/19 16:56, Chris Angelico wrote:
> On Wed, Dec 11, 2019 at 2:36 AM Antoon Pardon  wrote:
>> On 10/12/19 16:10, Chris Angelico wrote:
>>> On Wed, Dec 11, 2019 at 1:53 AM Antoon Pardon  wrote:
 What would you want to happen in the following case:

foo1 = Bar()
foo2 = foo1
del foo1

 Should the object be cleaned up now or not?

>>> TBH both are plausible,
>> I find that strange because if you cleanup the object in that scenario
>> it should also
>> be cleaned up in the following.
>>
>> def newbar():
>> foo = Bar
>> return foo
>>
>> bar = newbar()
>>
>> foo goes out of scope, so that is equivallent to a del foo. But I wouldn't
>> want the object cleaned up because of that.
>>
> Going out of scope isn't the same as explicit destruction.

In the context of this discussion it is. We are talking about about
automatic cleanup
and whether it should happen immediatly after a name is no longer
available. or wait
until the object is garbage collected. The fact that I used a del for
illustrating the
possible problems is just a detail.

That other languages may have different semantics with the del statement
is irrelevant.

-- 
Antoon Pardon
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Chris Angelico
On Wed, Dec 11, 2019 at 2:36 AM Antoon Pardon  wrote:
>
> On 10/12/19 16:10, Chris Angelico wrote:
> > On Wed, Dec 11, 2019 at 1:53 AM Antoon Pardon  wrote:
> >> What would you want to happen in the following case:
> >>
> >>foo1 = Bar()
> >>foo2 = foo1
> >>del foo1
> >>
> >> Should the object be cleaned up now or not?
> >>
> > TBH both are plausible,
>
> I find that strange because if you cleanup the object in that scenario
> it should also
> be cleaned up in the following.
>
> def newbar():
> foo = Bar
> return foo
>
> bar = newbar()
>
> foo goes out of scope, so that is equivallent to a del foo. But I wouldn't
> want the object cleaned up because of that.
>

Going out of scope isn't the same as explicit destruction. If you want
to simulate "going out of scope" without an actual scope change, the
nearest equivalent is something like "foo = None". In Python, there
isn't any such thing as "explicit destruction" (although
obj.__exit__() is often used for that kind of purpose), so the del
statement is just a removal from the current namespace; but when
explicit destruction does actually exist, it's an operation that
affects an object, not a name binding. (Any names previously bound to
that object will end up bound to the destroyed object.)

(Did you intend for that to be "foo = Bar()" ?)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Antoon Pardon
On 10/12/19 16:10, Chris Angelico wrote:
> On Wed, Dec 11, 2019 at 1:53 AM Antoon Pardon  wrote:
>> What would you want to happen in the following case:
>>
>>foo1 = Bar()
>>foo2 = foo1
>>del foo1
>>
>> Should the object be cleaned up now or not?
>>
> TBH both are plausible,

I find that strange because if you cleanup the object in that scenario
it should also
be cleaned up in the following.

def newbar():
foo = Bar
return foo

bar = newbar()

foo goes out of scope, so that is equivallent to a del foo. But I wouldn't
want the object cleaned up because of that.

-- 
Antoon Pardon.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Chris Angelico
On Wed, Dec 11, 2019 at 1:53 AM Antoon Pardon  wrote:
> What would you want to happen in the following case:
>
>foo1 = Bar()
>foo2 = foo1
>del foo1
>
> Should the object be cleaned up now or not?
>

TBH both are plausible, and to a Python programmer it's immediately
obvious that "foo1.close()" should close foo2 as well. Logic doesn't
preclude either of them. Imagine if the base object type had a
__destroy__() method that would dereference everything the object
referenced, clear its __dict__ (if it has one), and causes all
attribute access (including methods) to raise DestroyedObjectError. It
would be absolutely plausible to call foo1.__destroy__() and have the
object actually be destroyed.

I know this is plausible because I know of languages that work both
ways - that destroying an object actually destroys it immediately, or
that explicitly deleting a name binding leaves the object till it's
not referenced anywhere. And if there are languages working both ways,
it seems a little presumptive to heap scorn on a language designer for
either of those options.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Antoon Pardon
On 10/12/19 13:44, R.Wieser wrote:
> Chris,
>
>> Well, that's exactly what happens.
> So, only the reference count gets lowered.  Yep, thats daft.
>
> ... unless you explain why it can only be done that way.
>
>> Also, when you scorn something without knowing your facts,
>> you tend to make yourself look like a fool :)
> I tend to go with logic.   No logic => it most likely doesn't work that way.
>
> Choosing to go with a delayed action like you say will, as I already 
> explained, create problems later on (race conditions everywhere).  And as I 
> do not think the designers of the language where that stupid I must 
> therefore reject your claim to what you think happens.

Well yes that is why it is stupid to rely on the garbage collector to do
those things for you. That is why Greg Ewing already stated:

> If your object has a method for explicitly making it release its
> resources, it would be better to use that rather than rely on
> the garbage collector. 

> But, as you mentioned, I do not know (all) the facts.  Please do present 
> them.   Maybe there is a logic in there I do not yet see.

What would you want to happen in the following case:

   foo1 = Bar()
   foo2 = foo1
   del foo1

Should the object be cleaned up now or not?


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Python

R.Wieser wrote:

Chris,

...


But, as you mentioned, I do not know (all) the facts.  Please do present
them.   Maybe there is a logic in there I do not yet see.


We may, but then you wrote:


Check out the 'with' statement.


I'm sorry, but nope.

Rule one: When debugging do *not* throw more stuff at a problem than what is
neccessary to make it show its ugly head.
Rule two: When in doubt, see rule One. :-)


which contradicts your previous statement.

"with" is "more stuff" it is the right way to deal with disposable
ressource in the first place. In the documentation it is used from
the very first example of, say, open().


--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Chris Angelico
On Tue, Dec 10, 2019 at 11:46 PM R.Wieser  wrote:
>
> Chris,
>
> > Well, that's exactly what happens.
>
> So, only the reference count gets lowered.  Yep, thats daft.
>
> ... unless you explain why it can only be done that way.

Okay. What should happen when you do this?

x = 5
del x

Should the integer 5 be deleted?

> > Also, when you scorn something without knowing your facts,
> > you tend to make yourself look like a fool :)
>
> I tend to go with logic.   No logic => it most likely doesn't work that way.
>
> Choosing to go with a delayed action like you say will, as I already
> explained, create problems later on (race conditions everywhere).  And as I
> do not think the designers of the language where that stupid I must
> therefore reject your claim to what you think happens.
>
> But, as you mentioned, I do not know (all) the facts.  Please do present
> them.   Maybe there is a logic in there I do not yet see.
>
> > Check out the 'with' statement.
>
> I'm sorry, but nope.
>
> Rule one: When debugging do *not* throw more stuff at a problem than what is
> neccessary to make it show its ugly head.
> Rule two: When in doubt, see rule One. :-)

So the language designers couldn't possibly have been so stupid as to
do things this way, but you're going to ignore what they did? Cool.
Bye!

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread R.Wieser
Chris,

> Well, that's exactly what happens.

So, only the reference count gets lowered.  Yep, thats daft.

... unless you explain why it can only be done that way.

> Also, when you scorn something without knowing your facts,
> you tend to make yourself look like a fool :)

I tend to go with logic.   No logic => it most likely doesn't work that way.

Choosing to go with a delayed action like you say will, as I already 
explained, create problems later on (race conditions everywhere).  And as I 
do not think the designers of the language where that stupid I must 
therefore reject your claim to what you think happens.

But, as you mentioned, I do not know (all) the facts.  Please do present 
them.   Maybe there is a logic in there I do not yet see.

> Check out the 'with' statement.

I'm sorry, but nope.

Rule one: When debugging do *not* throw more stuff at a problem than what is 
neccessary to make it show its ugly head.
Rule two: When in doubt, see rule One. :-)

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Chris Angelico
On Tue, Dec 10, 2019 at 9:51 PM R.Wieser  wrote:
>
> Greg,
>
> > In that case, it was only working by accident.
>
> I don't think so.
>
> Or you must be thinking that the __del__ method of an instance is only
> called by the garbage collector - which would be rather daft (just think of
> a file thats created when initialising an instance, but not closes until the
> garbage collector comes around to it.   Much luck with reopening the file).
> Removing an unused object from memory is a bit different than
> closing/"destroying" an instance.

Well, that's exactly what happens. Daft as it might seem, the __del__
method IS only called when an object is disposed of by the garbage
collector. CPython happens to use a refcounting garbage collector that
will usually dispose of things fairly promptly, but in the case of
cyclic references, that won't happen till the cycle detection GC goes
through.

Check out the 'with' statement. Also, when you scorn something without
knowing your facts, you tend to make yourself look like a fool :)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread R.Wieser
Greg,

> In that case, it was only working by accident.

I don't think so.

Or you must be thinking that the __del__ method of an instance is only 
called by the garbage collector - which would be rather daft (just think of 
a file thats created when initialising an instance, but not closes until the 
garbage collector comes around to it.   Much luck with reopening the file). 
Removing an unused object from memory is a bit different than 
closing/"destroying" an instance.

> Python differs from VBScript and other VB-family languages in that
> names are case-sensitive.

I was aware of that.

> So it's common to use case to distinguish
> between things such as a class and an instance.

For some reason (the lacluster use of interpunction on tha intarwebz, or 
perhaps The Irritating Habit To Capitalise Every Word) I tend to largely 
ignore capitalisation.   Time will tell if I can cope with names that only 
differ by their capitalisation.   Most likely not.  Combine that with 
Pythons create-on-use behaviour and you have a recipe for disaster. :-|

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread Greg Ewing

On 10/12/19 9:45 pm, R.Wieser wrote:


In my case deleting the instance was needed to release its hold on something
that was taken when the instance was initialised (in my case a GPIO pin on a
Raspberry Pi).   I did not even think of garbage collection.


In that case, it was only working by accident. You were unwittingly
relying on the garbage collector to immediately clean up the object
as soon as the last reference to it disappeared. That happens to be
true currently in CPython (which uses reference counting), but it's
not guaranteed by the language.

If your object has a method for explicitly making it release its
resources, it would be better to use that rather than rely on
the garbage collector.


Than again, I normally camel-case my variable and function names (to make
them stand out from the build-in commands), as I normally do not use editors
with syntax highlighting.


Python differs from VBScript and other VB-family languages in that
names are case-sensitive. So it's common to use case to distinguish
between things such as a class and an instance.

--
Greg

--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-10 Thread R.Wieser
DL,

> I went through this cycle of thinking, first using if __name__... to 
> 'hide' testing, then adding test functions within the module, and now keep 
> test code quite separate from 'the real thing'.

:-) The normal newbie progression, off of which I'm in phase #1.   Having a 
working program (riddeled with debugging output) is still (way) more 
important than having the best code I'm afraid.   But reading about how 
others handle it definitily makes it easier for me to progress.

> Expanding on 'names' and del(): the first point is that there is seldom 
> much to be gained by using del(), ie becoming your own 'garbage 
> collector'.

In my case deleting the instance was needed to release its hold on something 
that was taken when the instance was initialised (in my case a GPIO pin on a 
Raspberry Pi).   I did not even think of garbage collection.

> Python enables you to play fast-and-loose, eg

I know (similar to VBScript which uses Variants for everything).   Its all 
about wnat I /intend/ to have inside it, and the type prefix character 
keeping myself aware of where (not) to use the variable.  Also, silent type 
conversions can work against you.I normally prefix an instanciated class 
(having methods) with an "o".

Than again, I normally camel-case my variable and function names (to make 
them stand out from the build-in commands), as I normally do not use editors 
with syntax highlighting.

> A Python-list will handle the former (wow, that's a bit of 'rocket 
> science'!?) whereas a Python-set will only contain unique values. So, 
> rather than using a somewhat-bland varNM such as "letters", I would choose 
> to be more explicit:

:-) Always start with a descriptive* name for the variable, regardless of if 
it is a compounded one or not.   As said, the type prefix is just a 
reminder.

*though not /too/ descriptive.   Languages which can easily have 
40-character names for variables, functions and such are simply the pits, 
especially when they start to differ only for a single/few character(s) 
somewhere in the middle. :-(

> rudy = Man( etc ) #rudy probably does have class, in this instance

You almost owed me a new keyboard there. :-p

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-09 Thread DL Neil via Python-list
It might be becoming 'long', but this discussion contains much of profit 
to many people!



Us 'silver surfers' do need to periodically question those beliefs we 
hold as 'tenets' of ComSc. Amongst them is RAM (or "core"!) 
conservation. It remains a virtue somewhat, but at the same time, 
storage costs are considerably cheaper than they were four decades ago, 
whereas the relative-cost of your time (to think about such things) has 
increased!


That said, many of our more youthful (and in their own words "better in 
every way", harrumph!) colleagues should also review their own beliefs 
from time-to-time, eg working with IoT devices certainly requires a more 
conservative and considerate approach to resources!
(and in such applications, we tend to enjoy significant advantage 
because we've 'been there, done that'!)



Similarly, it is a serious fallacy to attempt to directly 'translate' a 
'command' from one language into an 'equivalent' in another/a new 
language. Whilst the syntax may appear similar, such superficiality 
doesn't allow for subtleties of meaning! In the same way, if you ask one 
person to translate the English word "preserve" into German, and then 
another person to translate that German word into English, what are the 
chances that the 'answer' will be "preserve"? Given that "preserve" 
could mean a jam/jelly/conserve (noun) or (verb) 'maintain the meaning', 
with a similar range of synonyms (etc) on the German side; the word 
"preserve" is unlikely to be preserved!

(with all due apologies for such terrible humor and word-play!)


On 9/12/19 11:32 PM, Terry Reedy wrote:

On 12/9/2019 2:36 AM, R.Wieser wrote:

Terry,

Standard for in-file test is

[snip]

Any reason to put the testcode in a procedure instead of just have it
directly follow the "if __name__ == '__main__' " test ?


One can, for instance, interactively do

 >>> import mod
 >>> mod.test()

...

I went through this cycle of thinking, first using if __name__... to 
'hide' testing, then adding test functions within the module, and now 
keep test code quite separate from 'the real thing'.


In fact, PyTest (for example) will auto-magically look for test-code in 
sub-directories of the project directory, eg "projectNM/tests". So, for 
every (Python) file in my projectDIR, you should be able to find at 
least one corresponding Python test-file in the tests sub-dir!


This fits with your philosophy of keeping (imported) modules small, 
perhaps to the point of separating each class into its own module - if 
'efficiency' is the goal, why would you want to include test-code within 
a 'production system'?



On which point, if we had a class Person(); and then decided that in 
certain cases we wanted specific methods that didn't apply to every 
(type of) Person, we would likely create a sub-class, eg class Man( 
Person ); - although some might dispute the accuracy of such an "is a" 
relationship...


That being the case, were you to put the Man class' code into (say) a 
module called "man.py" and Person()'s code into "person.py", then a 
point made earlier is that you can't merely import man.py;! You must 
also ensure that person.py is imported, somewhere along the line. Thus, 
because it names (and requires the logic of) the super-class, the 
sub-class cannot be imported 'alone'. (regardless of saving-space 
objectives, etc)



Expanding on 'names' and del(): the first point is that there is seldom 
much to be gained by using del(), ie becoming your own 'garbage 
collector'. That said, there are some very specific times when it 
becomes absolutely necessary (otherwise the Python 'gods' would not have 
given us the functionality!). Once again, RAM is (relatively) plentiful, 
and when you need to replicate many multiples of an object, it is likely 
within a loop and therefore the same instanceNM is being 're-used' and 
the garbage-collector takes care of 'recycling' the storage space 
auto-magically.



Regarding 'names', yes some languages are "strongly typed" and (mostly) 
for that reason include an indicator of "type" within the name. Python 
enables you to play fast-and-loose, eg


>>> x = 1
>>> x = "abc"
>>> x = 2.3
>>> x = [ 1, 2, 3 ]
>>> x = ( 1, 2, 3 )
>>> x = 1, 2, 3

constitutes a perfectly legal program (where "legal" != "useful").


There again, there are times when values are processed into a different 
data structure, eg we start with a list of values, but only want to use 
unique values/process each representative-member once.


A Python-list will handle the former (wow, that's a bit of 'rocket 
science'!?) whereas a Python-set will only contain unique values. So, 
rather than using a somewhat-bland varNM such as "letters", I would 
choose to be more explicit:


>>> letters_list = [ "a", "b", "a", "c", "a", "d", "a" ]
>>> len( letters_list )
7
>>> letters_set = set( letters_list )
>>> len( letters_set )
4
>>> letters_set
{'b', 'a', 'd', 'c'}

There is no question whether "letters" is a list, or a set, (or ...), 

Re: Python3 - How do I import a class from another file

2019-12-09 Thread R.Wieser
Dennis,

> "del instance" only deletes the NAME.
[snip]

Yep, I already assumed that that would happen.  Just wanted to keep it 
simple.

> "instance" xyz does not shadow "class" xyz... instead, the name "xyz"
> only exists in the binding to the instance.

That is what I tried to say: the name-binding to the class is removed.

> However, that instance has a binding to the class object,

And than when the instance is deleted the binding to the class is lost, and 
as a result it is garbage-collected.  Right ?

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-09 Thread Python

Le 09/12/2019 à 09:15, R.Wieser a écrit :
...

You'll find that *any* form of import executes all the top-level code.


Thats certainly something I did not expect.


How could it be otherwise? Consider a module defining two
classes: Parent and Child, Child inheriting from Parent.

if you do "from the_module import Child", how could it
not execute the code defining Parent as well? Child
*depends* on Parent.


--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-09 Thread R.Wieser
Greg,

> There's nothing stopping you from subsequently rebinding that name to some 
> other object, or deleting it altogether.

Which is something I did not expect.  But running into is what makes doing 
something new interesting. :-)

And I just realized something else: I just assumed that the "del instance" 
command did something more than it should, but there is a good chance that 
the mere action of creating an instance with the same name as the class made 
the classname disappear from the internal availability list, and not return 
after the instance (and I assume its name too) got deleted.

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-09 Thread R.Wieser
Python,

> Child *depends* on Parent.

I'm sorry, but without any kind of underbuilding statements like that are of 
no value to me I'm afraid.

Also: if I can just copy-and-paste the class into the file which origionally 
included it and have the whole program run without a problem than that 
dependency upon its origional parent doesn't seem to mean all too much (if 
anything at all).

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-09 Thread Terry Reedy

On 12/9/2019 2:36 AM, R.Wieser wrote:

Terry,

Standard for in-file test is

[snip]

Any reason to put the testcode in a procedure instead of just have it
directly follow the "if __name__ == '__main__' " test ?


One can, for instance, interactively do

>>> import mod
>>> mod.test()

and explore the contents of mod, perhaps depending on the result of test()

def test(): ... also marks the code as test code rather than, for 
instance, command-line or setup code.  Putting such alternative code in 
function 'main' has the same effect.


For instance, 3.x has idlelib.__main__, which has something like

from idlelib.pyshell import main
main()

so than one can run 3.x IDLE from a command line with 'python -m idlelib'.

--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-09 Thread Greg Ewing

On 9/12/19 9:15 pm, R.Wieser wrote:

To be honest, I was not aware/did not assume that I could delete a class
definition that way (I even find it a bit weird, but as I'm a newbie in
regard to Python that does not say much).


Python is very dynamic. The class statement is actually an executable
statement that creates a class object and binds it to a name. There's
nothing stopping you from subsequently rebinding that name to some
other object, or deleting it altogether.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-09 Thread R.Wieser
Greg,

>>> Are you sure there's a difference between classes and functions here?
>>
>> Yes, quite sure.
>
> And we're even more sure that there isn't. :-)

As it turned out "we're" are right, and I was thrown off by the error 
message. :-p

The problem is that I had no idea why I got that (rather explicit) error 
message, and took it by its face-value - sending me of in the wrong 
direction.

The actual caise ?  I made a mistake by, in the testcode (which I had not 
blocked off) in the imported file creating an instance with the exact same 
name as the class.   The deleting of that instance at the end of the 
testcode somehow caused the class to disappear as well, resulting in the 
"can't find it" error.

To be honest, I was not aware/did not assume that I could delete a class 
definition that way (I even find it a bit weird, but as I'm a newbie in 
regard to Python that does not say much).

Bottom line: I was caught by my own choice to give the instance the exact 
same name as the class. :-\

>You'll find that *any* form of import executes all the top-level code.

Thats certainly something I did not expect.

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-09 Thread R.Wieser
DL,

> Some of this you will know. (Hopefully) some will be 'new':

Some of it is known to me, and indeed I do hope that there will be new stuff 
in there too.  Its why I took up Python. :-)

Thanks for the explanation (including the aliassing I wasn't aware of) and 
the links.

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-09 Thread R.Wieser
Terry,

> Not really.  The resulting global objects do not normally take enough 
> space to worry about on normal modern desktops.

For some reason I still aim to keep my (running) programs as small as 
possible - even though the ammount of memory has grown more than a 
thousandfold since I started programming.  Old habits die hard.

> Standard for in-file test is
[snip]

Any reason to put the testcode in a procedure instead of just have it 
directly follow the "if __name__ == '__main__' " test ?

> The PEP 8 convention (except for basic builtin data structures like int, 
> str, list, tuple, set, and dict) is TitleCase.

Thanks.

> Follow or not as you wish.

:-) If I ever want/need to share my code with others (or post here as part 
of a question) it is a good idea to conform to certain standards to keep it 
easily readable.  It will take some getting acustomed to though, as every 
language seems to have its own way to do it  (I myself prefer single prefix 
chars to indicate the variables intended type).

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-08 Thread Greg Ewing

On 9/12/19 6:28 am, R.Wieser wrote:



Are you sure there's a difference between classes and functions here?


Yes, quite sure.


And we're even more sure that there isn't. :-) Try it with
a test module containing a class definition, a function
definition and some top-level code that does something
noticeable. You'll find that *any* form of import executes
all the top-level code.

--
Greg

--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-08 Thread DL Neil via Python-list

On 9/12/19 7:29 AM, R.Wieser wrote:
...


Note that in all cases when you import a module (either by import the_file
or from the_file importe whatever) you actually import ALL of it


So much for my assumption only the class itself would be loaded - and a
wrench into my idea to have a number of classes in a "library" file.


There are three separate ideas which may be confusing:
1 we can 'load' a single class from a module, and
2 we can have 'library files' ("modules") which contains more than one 
importable-object

3 when a module is imported, parts of it are/may be executed at import-time

...


Update: While testing a bit more it turned out that, in the testcode, my
creating an instance with the same name as the class is to blame (who again
said that it isn't a good idea to use confusing names ?).  The moment I
changed the name of the instance everything works as expected.


and then there are the concepts of "scope" and "namespace".


Some of this you will know. (Hopefully) some will be 'new':


import moduleNM

- imports the entire contents of the module and executes what can be. 
This means that functions and classes are defined, and any executable 
code in the 'mainline', eg print( "Hello World" ); will be run.
- from that time you may then refer to the objects detailed within the 
module, eg


a = moduleNM.ClassA()

- note how the module has become part of the program, but occupies a 
separate "namespace"



from moduleNM import ClassA

- changes things so that you may now refer to ClassA without mention of 
the moduleNM, ie brings the name "ClassA" into the current namespace

- causes confusion if there is already a ClassA in the current namespace!


from moduleNM import ClassA as ClassA2

- also obviates the need to mention moduleNM/adds to the current namespace
- allows use of both ClassA-s - the 'local' ClassA as "ClassA", as well 
as providing an alias ("ClassA2") to the ClassA defined in moduleNM



import moduleNM as aliasNM; is also available!


WebRefs:
A comfortably readable expansion of the above: 
https://www.digitalocean.com/community/tutorials/how-to-import-modules-in-python-3
Python's view of (import-able) modules: 
https://docs.python.org/3/tutorial/modules.html
Other questions answered: 
https://docs.python.org/3/faq/programming.html?highlight=import, eg 
what-are-the-best-practices-for-using-import-in-a-module
The importlib library which under-pins "import": 
https://docs.python.org/3/reference/import.html
The full-fat range of import-possibilities: 
https://docs.python.org/3/library/modules.html--

Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-08 Thread Terry Reedy

On 12/8/2019 1:29 PM, R.Wieser wrote:



from the_file import ClassName


from somemod import name

has the same effect as

import somemod
name = somemod.name
del somemod

which is to import a particular 'name' linked to a particular python 
object into the namespace where the import is executed. 
sys.modules['somemod'] will become or continue to be the module 
resulting from executing the 'somemod' code.



Note that in all cases when you import a module (either by import the_file
or from the_file importe whatever) you actually import ALL of it


The entire module is executed the first time it is imported.


So much for my assumption only the class itself would be loaded - and a
wrench into my idea to have a number of classes in a "library" file.


Not really.  The resulting global objects do not normally take enough 
space to worry about on normal modern desktops.  It is normal for a 
module to only use a subset, possibly just one, of the objects in an 
imported module.  For instance, itertools has 18 public classes but 
almost no importer uses all of them.



if __name__ == '__main__':
 # not run when imported
 print("Hello world!")


Thanks for that.  It means I do not have to block-quote the testcode every
time - which I'm certain I will forget now-and-again ...


Standard for in-file test is

def test(): 

if __name__ == '__main__':
test()


Question: what is, in python, the convention in naming classes ?


The PEP 8 convention (except for basic builtin data structures like int, 
str, list, tuple, set, and dict) is TitleCase.  Follow or not as you wish.



Pre- or postfix it with "class" ?


No


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-08 Thread R.Wieser
Dennis,

> Common practice is that the class is capitalized. Instances
>are lowercase.

Thanks.

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-08 Thread R.Wieser
Peter,

>> "from file import function" works, but fails when I try to do the same
>> with a class.
>
> Are you sure? It should behave the same for any name in the module.

I am.

At least, that is what the error said: "cannot import 'classname' from 
'filename' "

But as it turns out the problem is not the importing, but that my importing 
of the class also caused my testcode (instanciating the class, calling it 
and deleting the instance afterwards) to run.

... in which I created an instance with the same name as the class. 
Changing the instances name made the error go away (probably something to do 
with the "del instance" in the testcode).

> It's all or nothing, you cannot cherry-pick specific classes or functions.

Shucks.

> You can however protect part of the code with
> 'if __name__ == "__main__": ...':

Thanks.  (Python" also mentioned the above two).

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-08 Thread R.Wieser
Python,

> from the_file import ClassName
>
> should work. I guess that your class name is not "class" right?

You guessed that right. :-)   Not a good idea to pick confusing names like
that, especially when you do something the first time.

> Note that in all cases when you import a module (either by import the_file
> or from the_file importe whatever) you actually import ALL of it

So much for my assumption only the class itself would be loaded - and a
wrench into my idea to have a number of classes in a "library" file.

And it turns out its also the cause of my problem: The displayed error told
me that it could not import the class from the file, but when I block-quoted
the testcode (instanciating the class, calling it and deleting the instance
afterwards) it all worked as it should.   No idea why though.

> if __name__ == '__main__':
> # not run when imported
> print("Hello world!")

Thanks for that.  It means I do not have to block-quote the testcode every
time - which I'm certain I will forget now-and-again ...

Update: While testing a bit more it turned out that, in the testcode, my 
creating an instance with the same name as the class is to blame (who again 
said that it isn't a good idea to use confusing names ?).  The moment I 
changed the name of the instance everything works as expected.

Question: what is, in python, the convention in naming classes ?   Pre- or 
postfix it with "class" ?  Something else ?

Regards,
Rudy Wieser



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-08 Thread R.Wieser
Chris,

> Are you sure there's a difference between classes and functions here?

Yes, quite sure.

If not my attempt to load a class he same way as a function would have 
worked.  Which it doesn't.

> Try "from file import function" and see if it runs commands at
> the root of the file.

What function ?   The imported file doesn't contain any.

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-08 Thread Peter Otten
R.Wieser wrote:

> Hello all,
> 
> Using Python3 I would like to import a specific class from another file
> (in the same folder), and have trouble doing so.
> 
> "from file import function" works, but fails when I try to do the same
> with a class.

Are you sure? It should behave the same for any name in the module.
 
> "import file
> x = file.class"
> 
> works, but also executes commands that are in the root of the file. And
> ofcourse loads all of the file, not just the class that I'm after ...
> 
> In other words, what is the syntaxt I should use ?

The body of a module that is imported is always executed -- that's how the 
classes and functions (and everything else) in the module are created.
It's all or nothing, you cannot cherry-pick specific classes or functions.

You can however protect part of the code with 
'if __name__ == "__main__": ...':


print("this will always be executed")
if __name__ == "__main__":
   print("this will only be executed when you run")
   print("the module directly as a script")


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-08 Thread Python

R.Wieser wrote:

Hello all,

Using Python3 I would like to import a specific class from another file (in
the same folder), and have trouble doing so.

"from file import function" works, but fails when I try to do the same with
a class.

"import file
x = file.class"

works, but also executes commands that are in the root of the file. And
ofcourse loads all of the file, not just the class that I'm after ...


from the_file import ClassName

should work. I guess that your class name is not "class" right?

Note that in all cases when you import a module (either by
import the_file or from the_file importe whatever) you actually
import ALL of it (even if you don't see ALL names your name
space). If you do not want part of a module to be executed when
it is imported (any ways) but only when the file is executed
as a script (i.e. python3 the_file.py or ./the_file.py) then
you can test the name __name__ : it is a string "__main__"
when it is executed and the module name when imported :

if __name__ == '__main__':
# not run when imported
print("Hello world!")

--
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-08 Thread Chris Angelico
On Mon, Dec 9, 2019 at 3:31 AM R.Wieser  wrote:
>
> Hello all,
>
> Using Python3 I would like to import a specific class from another file (in
> the same folder), and have trouble doing so.
>
> "from file import function" works, but fails when I try to do the same with
> a class.
>
> "import file
> x = file.class"
>
> works, but also executes commands that are in the root of the file. And
> ofcourse loads all of the file, not just the class that I'm after ...
>
> In other words, what is the syntaxt I should use ?
>

Are you sure there's a difference between classes and functions here?
Try "from file import function" and see if it runs commands at the
root of the file.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Python3 - How do I import a class from another file

2019-12-08 Thread R.Wieser
Hello all,

Using Python3 I would like to import a specific class from another file (in 
the same folder), and have trouble doing so.

"from file import function" works, but fails when I try to do the same with 
a class.

"import file
x = file.class"

works, but also executes commands that are in the root of the file. And 
ofcourse loads all of the file, not just the class that I'm after ...

In other words, what is the syntaxt I should use ?

Regards,
Rudy Wieser


-- 
https://mail.python.org/mailman/listinfo/python-list