title: Define a python function which takes arguments but also works with no argument
body: <p>I'm trying to wrap the built-in function <code>dir()</code> to sort its results. If I define and pass an argument to it, it works fine. However if I try to get it to show the root namespace, it won't work because it either requires an argument or gets an empty object which it returns attributes for instead.</p>
  
  <p>I've tried redefining it with <code>def dirs(*function):</code> argv instead, but that returns a list, not the current scope either.</p>
  
  <p>Is there a way to wrap this built-in with the same functionality? I don't know C so the built-in source code wasn't much help to me, searching shows a lot of results for files in directories, but I can't seem to find how to crack this one.</p>
  
  <pre><code>def dirs(function):
      print '{}'.format(type(function))
      try:
          for i in sorted(dir(function)):
              print '{}'.format(i)
      except:
          for i in sorted(dir()):
              print '{}'.format(i)
  </code></pre>
  
  <p>I've tried all of the data types I could think of, but I can't replicate the behaviour of the reference behaviour below.</p>
  
  <pre><code>print '# first for reference, a sorted dir()'
  for i in sorted(dir()):
      print '{}'.format(i)
  print '# next up: double quoted string'
  dirs("")
  print '# next up: None'
  dirs(None)
  print '# next up: empty dictionary'
  dirs({})
  print '# next up: empty set'
  dirs(())
  print '# next up: empty list'
  dirs([])
  </code></pre>
  
  <p>Here's a sample of the output, truncated for brevity.</p>
  
  <pre><code># Result:
  # first for reference, a sorted dir()
  FnOpenPluginInstaller
  __allcompletions
  [...]
  tools_dir
  windowContext
  # next up: double quoted string
  &lt;type 'str'&gt;
  __add__
  __class__
  [...]
  upper
  zfill
  # next up: None
  &lt;type 'NoneType'&gt;
  __class__
  __delattr__
  [...]
  __str__
  __subclasshook__
  # next up: empty dictionary
  &lt;type 'dict'&gt;
  __class__
  __cmp__
  [...]
  viewkeys
  viewvalues
  # next up: empty set
  &lt;type 'tuple'&gt;
  __add__
  __class__
  [...]
  count
  index
  # next up: empty list
  &lt;type 'list'&gt;
  __add__
  __class__
  [...]
  reverse
  sort
  
  </code></pre>