This is a continuation of the previous Super Basics. This time, I will summarize the differences and points to note about functions.
As I explained in Super Basics, it's a review.
First of all, PHP code.
<?php
function func() {
echo 'hoge';
}
func();
This is the Python code.
# coding=utf8
def func(): #Last: (colon)Don't forget!
print('hoge')
func()
PHP uses function, but Python uses def.
Python does not use {} to represent scopes, but uses indent.
The basics of PHP and Python are not that different.
However, Python's functions have something called keyword arguments.
Let's take an example.
If you are using PHP, you may come across a function that has a large number of arguments that have a default value.
Then, if you want to set only the last argument to a value other than the default value, you have to write as follows.
<?php
function func($hoge='hoge', $piyo='piyo', $foo='foo', $bar='bar') {
echo $hoge . PHP_EOL;
echo $piyo . PHP_EOL;
echo $foo . PHP_EOL;
echo $bar . PHP_EOL;
}
//If you want to set the 4th value to something other than the default value, you have to write the 1st to 3rd items as well.
func('hoge', 'piyo', 'foo', 'fizz');
The keyword argument is useful in such cases!
Only the specified arguments can be passed by writing in a special way that PHP does not have.
# coding=utf8
def func(hoge='hoge', piyo='piyo', foo='foo', bar='bar'):
print(hoge)
print(piyo)
print(foo)
print(bar)
#You can pass only the specified place!
func(bar='fizz')
When implementing a function with variable length arguments, PHP looks like this:
<?php
function func() {
$args = func_get_args();
var_dump($args);
}
func('hoge');
func('hoge', 'piyo');
Expressing this in Python looks like this!
# coding=utf8
def func(*args):
print(args)
func('hoge')
func('hoge', 'piyo')
Just add * to the beginning of the argument name.
The argument name ʻargs is not fixed and can be any name. The ʻargs variable can be treated as a immutable array (tuple).
It's very simple.
You can also receive the keyword argument that came out earlier with a variable length.
# coding=utf8
def func(**kwargs):
print(kwargs)
func(hoge='hoge')
func(hoge='hoge', piyo='piyo')
For the keyword argument, just add**to the beginning of the argument name.
In this case, it can be treated as a dictionary of pairs of argument names and values instead of tuple.
You can even use them together!
# coding=utf8
def func(*args, **kwargs):
print(args)
print(kwargs)
func('hoge')
func('hoge', piyo='piyo')
You can use lists (tuples) and dictionaries when passing values to functions.
# coding=utf8
def func(hoge, piyo):
print(hoge)
print(piyo)
args = ['foo', 'bar']
#The order of the arguments corresponds to the order of the list.
func(*args)
It's similar to the case of variable length arguments.
By adding * to the name of the variable in which the list is stored and passing it to the function, you can handle the list element as an argument.
# coding=utf8
def func(hoge, piyo):
print(hoge)
print(piyo)
kwargs = {
'hoge': 'foo',
'piyo': 'bar',
}
func(**kwargs)
Similar to the list method, if you add**to the name of the variable that stores the dictionary and pass it to the function, the value will be assigned to the argument corresponding to the key (subscript). To go.
However, please note that if you write an argument name that does not exist in the key (subscript), an error will occur. ..
I will introduce the part that I was a little addicted to. First look at the PHP code.
<?php
function func($hoge=array('foo')) {
#in hoge array'bar'Added the element.
$hoge[] = 'bar';
var_dump($hoge);
}
func();
func();
The function that adds 'bar' to$ hoge = array ('foo')and outputs it is called twice.
I think that the execution result of each function will be the same.
When this is written in Python, it looks like the following.
# coding=utf8
def func(hoge=['foo']):
#on the hoge list'bar'Added the element.
hoge.append('bar')
print(hoge)
func()
func()
In fact, this Python code ** doesn't work as expected! !! !! !! !! ** ** Those who actually executed and compared them should have confirmed that the execution results are different. I'll leave the detailed explanation to others, but here ** Do not use a list / dictionary as the default value of the argument! Please remember **.
Python functions are very flexible.
Especially the keyword argument is powerful, so please remember it.
Next, Explanation of classes!
Recommended Posts