Python3 - How do I import a class from another file

Rhodri James rhodri at kynesim.co.uk
Tue Dec 10 13:44:52 EST 2019


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


More information about the Python-list mailing list