python learning notes 2.
function
It’s similiar to c in function. One thing to mention, library function the return value of hex() is a str not a number. So you can do abs() on hex(num).
define your own function
syntax:
def functionName(parameter):
# do something
return value
Return value is none by default. And if we write this function in a fileName.py file, we can use from fileName import functionName
to import the function we want.(This two files must be in the SAME PATH).
empty function
If we want to define a function, but don’t give it anything to do right now(you may complete this function later).In c, you can just use a brace then leave the whole function content to be empty. Compiler won’t report any error. But this way doesn’t make sense in python. You need to add pass
to this function to define an empty function.Just like this:
def emptyFunc()
pass
pass
here represent a placeholder. It tells compiler: just pass me. Also we can use pass in if statement.
parameter check
For in-built function, if we pass a wrong type to it, it will reports TypeError
but if we don’t check passing parameters, compiler won’t report exact error information. So, we should do parameters check when we define a function. isinstance
is such a in-built function for parameter checking. e.g.
def func(x)
if not isinstance(x,(float,int)):
raise TypeError('bad operand type')
# do something
return multi-value
In c, we can only return one value. If we truly want to return at least two value, returning a struct is a common method. However, in python we don’t need to worry about that thing. Just return x,y. That’s ok. Compiler will automatically accumalate these values to a tuple then return this tuple.
parameter
Parameter in python is much more complicated than in c. It has 5 kind of parameters. Let’s go through it one by one.
- positional parameter && default parameter.
These two parameter types are also used in c. Skip it. - mutable parameter
This parameter type allows us to pass multiple values to function. Just think it as passing a tuple or list to the function. However, someone thinks it’s too inconvienent, thus we add multiple parameter. Just add a * in front of this parameter when you define it. Then, compiler will help us to transform these parameters to tuple/list. What’s more, when we want to pass a tuple or list to this multiple parameter, just add a * in front of the name of this tuple/list.(you may think * here is quite similiar to some kind of address in c). - keyword parameter
Alike multiple parameters, python also provides keyword parameters with a inner structure using dict. Use **wk to represent that this is a keyword parameter that receive pair of undefined parameters. e.g.
func('chen',18,job='student',city='hefei')
# and func is defined like this
def func(name,age,**wk)
# Here, wk is keyword parameter.
- named keyword parameter.
In keyword parameter, we don’t limit what parameters we can pass, that’s to say, you can pass all kinds of strange value to this function. Hence, we sometimes need to limit the name of parameters. Using * to represent that this function receieve specified parameter name. e.g.
def func(name,age,*,city,job)
Now, users can only pass extra parameters when the parameter name is city or job.