[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Python3 - How do I import a class from another file

On Wed, Dec 11, 2019 at 2:36 AM Antoon Pardon <antoon.pardon at vub.be> wrote:
> On 10/12/19 16:10, Chris Angelico wrote:
> > On Wed, Dec 11, 2019 at 1:53 AM Antoon Pardon <antoon.pardon at vub.be> 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()" ?)