-2

Since I am new to programming, I just want to understand the format the built-in functions is represented in the documentations. This is the one from python

bytearray ([source[, encoding[,errors]]])

Why all the square brackets are terminated at the end? The brackets are nested. There is comma immediately after the square bracket too. Does it mean it takes only 3 arguments? Is the below same as the above one?

bytearray ([source],[encoding],[errors])

I am not understanding the format it represents.

2
  • Stuff being in square brackets means it's optional. It being nested means it's optional, but the outer levels of nesting must be there if it is.
    – TZHX
    Commented Feb 25, 2017 at 15:11
  • 1
    Cross-site dupe: stackoverflow.com/questions/1718903/…. Please research before asking.
    – jonrsharpe
    Commented Feb 25, 2017 at 16:47

1 Answer 1

4

Brackets means something is optional. For instance foo[bar]qux could be either fooqux or foobarqux. You could treat it like a regular expression foo(bar)?qux in your head if that helps.

Thus bytearray ([source[, encoding[,errors]]]) could be any one of

  • bytearray(source, encoding, errors)
  • bytearray(source, encoding)
  • bytearray(source)
  • bytearray()

This also shows that you need to open the brackets before the comma, and close them all at once. With your suggested bytearray([source], [encoding], [errors]), the options would be:

  • bytearray(source, encoding, errors)
  • bytearray(source, , )
  • bytearray(, , errors)
  • bytearray(, , )
  • etc. (There are eight possibilities in total, I'm not going to type them all out.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.