0

How do i detect <class 'bytes'> objects in Python?

I want something along the lines of

 if type(x) == bytes:
     doesomething(x)
2
  • 1
    @Fredrik: Do you even understand what is does? Commented Jul 17, 2016 at 22:22
  • Did you try the code you posted? It should work. But as the answer below notes, is would be preferred to ==. Commented Jul 17, 2016 at 22:27

1 Answer 1

2

You can simply do:

if type(x) is bytes

Or:

if isinstance(s, bytes)
Sign up to request clarification or add additional context in comments.

1 Comment

the preferred form is isinstance - as it works with subclassing and "virtual" subclassing using ABCs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.