cydia
cydia

Reputation: 113

The way with the variable name is not the same as the way with no variables?

s = requests.Session()
a1=s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')


a2 = requests.Session().get('http://httpbin.org/cookies/set/sessioncookie/123456789')

Why is a1 != a2?
According to my understanding, a1 and a2 are equal, but in fact a1 and a2 are not equal?

Upvotes: 3

Views: 42

Answers (2)

Sebastian Schneider
Sebastian Schneider

Reputation: 5542

With that comparrison you would compare the instances of your classes, not the values.

For better understanding:

a1 is a new instance of Session with some id (e.g. 12345) which requested some url

a2 is another instance of Session with some id (e.g. 56789), however not the same!

a1 == a2 is equivalent to 12345 == 56789

to compare the values, you have get set the variables to the output of your desired function. e.g.:

a1.json() == a2.json()

Upvotes: 1

Mel
Mel

Reputation: 6065

Using the same way would not work either:

>>> a1 =  requests.Session().get('http://httpbin.org/cookies/set/sessioncookie/123456789')
>>> a2 =  requests.Session().get('http://httpbin.org/cookies/set/sessioncookie/123456789')
>>> a1 == a2
False

That's because request.Session().get() returns a class instance

>>> type(a1)
<class 'requests.models.Response'>

You usually can't directly compare class instances, unless the comparison has been implemented in the class code.

You could compare the json responses:

>>> a1.json() == a2.json()
True

Upvotes: 1

Related Questions