Section | Video Links |
---|---|
Bridge Overview | |
Bridge Use Case | |
Python Tuple | |
Python *args |
... Refer to Book or Design Patterns In Python website to read textual content.
... Refer to Book or Design Patterns In Python website to read textual content.
... Refer to Book or Design Patterns In Python website to read textual content.
python ./bridge/bridge_concept.py
('a', 'b', 'c')
a
b
c
... Refer to Book or Design Patterns In Python website to read textual content.
python ./bridge/client.py
******
** **
* *
* *
* *
* *
** **
******
**************
* *
* *
* *
* *
* *
* *
**************
The *args
argument takes all arguments that were sent to this method, and packs them into a Tuple.
It is useful when you don't know how many arguments, or what types, will be sent to a method, and you want the method to support any number of arguments or types being sent to it.
If you want your method to be strict about the types that it can accept, the set it specifically to accept List, Dictionary, Set or Tuple, and treat the argument as such within the method body, but the *args
argument is another common option that you will see in source code throughout the internet.
E.g., when using the *args
in your method signature, you can call it with any number of arguments of any type.
def my_method(*args):
for arg in args:
print(arg)
my_method(1, 22, [3], {4})
Outputs
1
22
[3]
{4}
A Python Tuple is similar to a List. Except that the items in the Tuple are ordered, unchangeable and allow duplicates.
A Tuple can be instantiated using the round brackets ()
or tuple()
, verses []
for a List and {}
for a Set or Dictionary.
PS> python
>>> items = ("alpha", "bravo", "charlie", "alpha")
>>> print(items)
('alpha', 'bravo', 'charlie', 'alpha')
>>> print(len(items))
4
... Refer to Book or Design Patterns In Python website to read textual content.