3

I often find some functions defined like open(name[, mode[, buffering]]) and I know it means optional parameters.
Python document says it's module-level function. When I try to define a function with this style, it always failed.
For example
def f([a[,b]]): print('123')
does not work.
Can someone tell me what the module-level means and how can I define a function with this style?

7
  • 2
    i think you are speaking about the doc representation, like it was for python2, maybe the python3 doc is more clear ? It's about optional parameters, like def f(a=None, b=None) Commented Sep 9, 2017 at 6:57
  • See this answer to Function with optional arguments?. Commented Sep 9, 2017 at 7:00
  • 4
    The square bracket notation is not code, it is a way of describing language syntax called Backus-Naur form. It is used extensively for the documentation of many languages, not just python. en.wikipedia.org/wiki/Augmented_Backus-Naur_form Commented Sep 9, 2017 at 7:01
  • 2
    It seems there is no real coding with this style, so the doc representation is just representation and it does not represent the real code? Commented Sep 9, 2017 at 7:09
  • exactly ! Now what was your goal, to write a function accepting optional parameters ? Do you know some ways to achieve that ? Commented Sep 9, 2017 at 7:15

2 Answers 2

2

Is this what you are looking for?

>>> def abc(a=None,b=None):
...  if a is not None: print a
...  if b is not None: print b
... 
>>> abc("a")
a
>>> abc("a","b")
a
b
>>> abc()
>>> 
Sign up to request clarification or add additional context in comments.

7 Comments

Not a good idea to equate None with False in this case. What if a or b had the value 0 (zero) or "" (empty string)?
!= has its dangers as well. Test for not None might be better as if not a is None:
@cdarke "comparisons to singletons like None should always be done with is or is not, never the equality operators." Thank you, we live and learn.
By default python uses a memory reference to check equality of Object instances (different than primitives). However, operators in python can be overwritten such as __eq__. This means that a and b are not guaranteed to be a simple reference check. It could possibly try to access a field within the None instance and then error out. The is keyword on the other hand will only ever do a reference test- removing the potential error. (it will also perform quicker as a result of the simpler operation)
|
1
  1. "if we can define optional parameters using this way(no at present)"

    The square bracket notation not python syntax, it is Backus-Naur form - it is a documentation standard only.

  2. A module-level function is a function defined in a module (including __main__) - this is in contrast to a function defined within a class (a method).

Comments

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.