Jessica
Jessica

Reputation: 2415

"type" command in python gives unexpected results

I ran the following code in the Python interpreter:

>>> s
<sqlalchemy.orm.session.Session object at 0x7f4fc8d69e10>
>>> type(s)
<class 'sqlalchemy.orm.session.Session'>
>>> type(s) == sqlalchemy.orm.session.Session
False

(1) Why does the interpreter say the type of s is not sqlalchemy.orm.session.Session, when it just said that it was?

(2) If the type command doesn't work, what is the best way to tell if an object is a Session object? (I'm trying to use this in an assertion to validate the input to a function)

(3) Just for fun, I tried to do this with a primitive type, and it worked. Why are integers different here?

>>> type(3)
<type 'int'>
>>> type(3) == int
True

Upvotes: 1

Views: 73

Answers (1)

user2357112
user2357112

Reputation: 280465

Your s is actually an instance of a subclass of sqlalchemy.orm.session.Session. The subclass is confusingly also named sqlalchemy.orm.session.Session.

Looking at the SQLAlchemy source, it seems likely that this object was produced by a sqlalchemy.orm.session.sessionmaker, which creates a subclass of Session for reasons I'm not entirely clear on. I don't know if this behavior is documented; I didn't find any mention of it in the docs on a quick look.

Upvotes: 2

Related Questions