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? 
            Asked
            
        
        
            Active
            
        
            Viewed 2,109 times
        
    3
            
            
         
    
    
        chenkh
        
- 69
- 1
- 9
- 
                    2i think you are speaking about the doc representation, like it was for [python2](https://docs.python.org/2/library/functions.html?highlight=open#open), maybe the [python3](https://docs.python.org/3/library/functions.html?highlight=open#open) doc is more clear ? It's about optional parameters, like `def f(a=None, b=None)` – PRMoureu Sep 09 '17 at 06:57
- 
                    See this answer to [Function with optional arguments?](https://stackoverflow.com/questions/9539921/function-with-optional-arguments#9539977). – jq170727 Sep 09 '17 at 07:00
- 
                    4The 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. https://en.wikipedia.org/wiki/Augmented_Backus-Naur_form – cdarke Sep 09 '17 at 07:01
- 
                    2It seems there is no real coding with this style, so the doc representation is just representation and it does not represent the real code? – chenkh Sep 09 '17 at 07:09
- 
                    exactly ! Now what was your goal, to write a function accepting optional parameters ? Do you know some ways to achieve that ? – PRMoureu Sep 09 '17 at 07:15
- 
                    As to your question about what "module-level" means in what you read, it's hard to say with any specificity not knowing the context, but rest assured it's completely unrelated to what it seems like you *really* want to know (how to allow for functions to accept optionally-provided arguments). (A general, and probably not helpful, description might be that module-level code -- and module-level functions -- appear inside modules) – jedwards Sep 09 '17 at 07:35
- 
                    **Surely** this question must be a duplicate? But I can't find an appropriate question with a matching answer. (Yeah I saw the link posted by @jq. But do **not** [use a `*args` parameter](https://stackoverflow.com/questions/9539921/function-with-optional-arguments#9539977) whenever you define a function with optional arguments! This is not the 1980s). That answer solves a different problem. – alexis Sep 09 '17 at 09:31
2 Answers
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()
>>> 
 
    
    
        Rolf of Saxony
        
- 21,661
- 5
- 39
- 60
- 
                    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)? – cdarke Sep 09 '17 at 07:40
- 
                    `!=` has its dangers as well. Test for not `None` might be better as `if not a is None:` – cdarke Sep 09 '17 at 07:44
- 
                    @cdarke Blimey instantaneous comment. I'd be happy to change it again, if you could enlighten me as to why `!= None` is dangerous – Rolf of Saxony Sep 09 '17 at 07:50
- 
                    1About `is None` vs. `== None`: https://stackoverflow.com/questions/14247373/python-none-comparison-should-i-use-is-or, https://stackoverflow.com/questions/2209755/python-operation-vs-is-not, https://www.python.org/dev/peps/pep-0290/#testing-for-none, https://www.python.org/dev/peps/pep-0008/#programming-recommendations – jeremye Sep 09 '17 at 08:08
- 
                    1@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. – Rolf of Saxony Sep 09 '17 at 08:17
- 
                    1By 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) – flakes Sep 09 '17 at 08:23
- 
                    An similar case in the Java world is that `==` does a reference test whereas `.equals` will do a custom comparison. This makes `==` the preferred choice for `null` checking in Java, just like how `is` is the preferred choice in python – flakes Sep 09 '17 at 08:33
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. 
- 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).
 
    