0

I am currently learning the basics of networking in Python and keep seeing this syntax, a variable, followed by another variable, but separated with a comma.

conn, address = s.accept()

I understand the s.accept(), I am just unsure of the variable that I am assigning the result of s.accept() to. When entering:

type(conn, address)

I am returned with a Tuple but I do not understand how the conn and address play any part in a Tuple, and was wondering if I could receive some guidance as to what this syntax means.

Thanks in advance, Ollie.

Ollie Pugh
  • 373
  • 2
  • 15

2 Answers2

2

s.accept() returns a tuple of two values : (host, port).

Therefore,

conn, address = s.accept()

is (apart that accept() is called twice) the same as

conn, address = s.accept()[0], s.accept()[1]
Fukiyel
  • 1,166
  • 7
  • 19
0

when receiving a tuple you can unpack(or "split") it into its members by using this syntax:

member1, member2, member3 = tuple

or

member1, member2 member3 = (member1, member2 member3)

in your case you are receiving a tuple of the form (connection, address), so to unpack it into two variables you write:

conn, address = s.accept()

It's equivalent to this:

returned_tuple = s.accept()
conn, address = returned_tuple
Adam
  • 16,808
  • 7
  • 52
  • 98
Shay Lempert
  • 311
  • 2
  • 9