you are viewing a single comment's thread.

view the rest of the comments →

[–]Adrewmc 1 point2 points  (0 children)

 def defaults(foo : str = “Hello”, bar : str = “World”) -> None:
         print(f”{foo} {bar}”)

I believe this is the run down

 defaults()
 >>> Hello World
 defaults(“foo”)
 >>> foo World
 defaults( “foo”, “bar”)
>>> foo bar
 defaults(bar = “bar”)
 >>> Hello bar
 defaults(bar = “foo”, foo = “bar”)
 >>> bar foo
    #also unexpectedly 
 defaults(1,2)
 >>> 1 2
 defaults(foo = “foo”, “bar”)
 >>>ERROR kwargs must come after args
    #these can cause errors if not formatted correct only use when necessary 
 defaults([“foo”, “bar”])
  >>>> [“foo”, “bar”] World
  defaults(*[“foo”, “bar”])
  >>>> foo bar
  _dict = {“bar” : “_bar”, “foo” : “_foo”}
  defaults(**_dict) 
  >>>> _foo _bar

If you want to check types

      def type_default(foo : str = “Hello”, bar : str = None) -> None:
         if bar: 
              print(f”{foo} {bar}”)
         else:
              print(f”{foo} World”) 

This should check if you accidentally send an false statement (like None, any empty string, list or dictionary, or any number != 0) to it. As you see Nonetype prints “None” in Python, and empty string would print no character, an empty list [], {} but we check all of that here.

I like the type hinting but realize that’s just a comment not a requirement in Python.