type vs. module (part2)

S

Shane

In the following t,t1 are the result of built-in call type() -- the
form that takes three arguments.
Therefore they are classes. Consider the following output:

print type(t)
<class 'a.b.f.F'> print id(t)
1234567 print t.__module__
a.b.t.d

print type(t1)
<class 'a.b.f.F'> print id(t1)
1234568 print t1.__module__
a.b.t.d

I now have two questions: How does Python allow two classes of the
same
type as evidenced by identical ``print type(<class>)' output and
different id
outputs?

Also, which module is t,t1 actually in? Is it "a.b.f"? Or is it
"a.b.t.d".

I am totally confused.
 
A

alex23

I now have two questions: How does Python allow two classes of the
same
type as evidenced by identical ``print type(<class>)' output and
different id
outputs?

You are looking at the id of two _instances_ of the class, not of the
class itself.
.... pass
....(25931760, 25968816)
Also, which module is t,t1 actually in? Is it "a.b.f"? Or is it
"a.b.t.d".

Which module did you declare them in? What makes you think they're
defined somewhere other than what .__module__ is telling you?

My guess is your class is in a.b.f, your instances are created in
a.b.t.d, and you've demonstrated very powerfully the value of
meaningful names in code.
I am totally confused.

And you have the source code. Imagine how we feel.
 
S

Steven D'Aprano

In the following t,t1 are the result of built-in call type() -- the form
that takes three arguments.

Are you sure about that? Because my explanation below will depend
entirely on this alleged fact: that t and t1 themselves are classes, not
instances.

To be sure (well, *nearly* sure), please print t and t1 and see whether
you get something like:

print t
=> <class 'blah-blah-blah'>

or

print t
=> <blah-blah-blah object at 0x9c5c64c>


Therefore they are classes. Consider the following output:

print type(t)

print type(t1)

I now have two questions: How does Python allow two classes of the same
type as evidenced by identical ``print type(<class>)' output and
different id outputs?

When you use the class statement, you end up with one class with one
name, but that's not a hard rule about classes. It's just a convention.


You can have multiple identical classes so long as you assign them to
different names. For example:
.... pass
........ x = 1
....__main__.Spam __main__.Spam


What's going on here? The secret is that classes (and functions!)
generally have *two* names. The first name is their internal name, the
name they report when you print them. The second is the name of the
variable (or variables!) they are bound to. Nobody says that they MUST
match, although they often do.


When you use the class statement, Python's interpreter ensures that you
start off with the two names matching, but as you can see from above,
they don't necessarily stay matching. But the type() function doesn't
enforce that:
<class '__main__.cheese'> <class '__main__.cheese'>

Both brie and cheddar know their own name as "cheese", but the names you
use to refer to them are different.



So, bringing this back to your examples...

Both t and t1 are classes, both know their internal name as "F", but they
are different classes, as seen by the fact that their IDs are different.

Also, which module is t,t1 actually in? Is it "a.b.f"? Or is it
"a.b.t.d".

Since they are two independent classes, it could easily be "both, one for
each".
 
S

Steven D'Aprano

So, bringing this back to your examples...

Both t and t1 are classes, both know their internal name as "F", but
they are different classes, as seen by the fact that their IDs are
different.

Oops, no, sorry, a mistake... assuming you are correct that both t and t1
are classes, we don't have enough information to know what they believe
they are called. (Printing t and t1 should tell you.) What we know is
that their *metaclass* (the class of a class) knows itself as "F".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top