Custom function learning notes and Python learning notes in python

Source: Internet
Author: User

Custom function learning notes and Python learning notes in python

Define a function that does nothing
Copy codeThe Code is as follows:
>>> Def ():
... Pass
...

>>> Def printHello ():
... Print ("hello ")
...
>>> PrintHello ()
Hello
>>> Callable (printHello)
True

As the name implies, the callable function is used to determine whether a function can be called;

Some books say that callable is no longer used in Python3.0, instead of hasattr (func, '_ call;
Copy codeThe Code is as follows:
>>> Hasattr (printHello, '_ call __')
True

>>> PrintHello. _ doc __
>>> Def printHello ():
... 'Just print hello'
... Print ('hello ')
...
>>> PrintHello. _ doc __
'Just print hello'

Each function has a _ doc _ attribute. Double underscores indicate that it is a special attribute;

Built-in help functions are very useful and provide help information about methods/functions;
Copy codeThe Code is as follows:
>>> Help (printHello)

The comments of the function include;

Although the printHello function does not use return, you can use a variable to receive the return value:
Copy codeThe Code is as follows:
>>> Result = printHello ()
Hello
>>> Result
>>> Print (result)
None

None is the built-in value of Python. Is None similar to Javascript's undefined?

Defines a printStr that can receive parameters to print strings.
Copy codeThe Code is as follows:
>>> Def printStr (str ):
... Print (str)
...

>>> PrintStr ("hello ")
Hello

Like C ++, Python supports default parameters.
Copy codeThe Code is as follows:
>>> Def printStr (str = "nothing "):
... Print (str)
..

>>> PrintStr ()
Nothing

Let's take a look at the parameter passing method.
Copy codeThe Code is as follows:
>>> A = [1, 2]
>>> Def try_change_list ():
... A [:] = [1, 1]
...
>>> Try_change_list ()
>>>
[1, 1, 1]

The passing parameter of Python can be understood as passing by value (same as java and Javascript )?

BTW: if you do not want try_change_list to change the original object, you can pass a [:]
Copy codeThe Code is as follows:
>>> A = [1, 2]
>>> Try_change_list (a [:])
>>>
[1, 2]

Of course, here we are doing a small copy. We can use the deepcopy of the copy module for deep copy;

In addition to the default parameter values, the following parameters can also be named:
Copy codeThe Code is as follows:
>>> Def sum (a = 0, B = 0 ):
... Return a + B;
...
>>> Sum (2, 2)
4
>>> Sum (B = 3, a = 4)
7

This feature is easy to use when many parameters exist;

Let's take a look at Python's support for the variable parameter list:
Copy codeThe Code is as follows:
>>> Def sum (* args ):
... S = 0;
... For I in args:
... S + = I;
... Return s
...
>>> Sum (1, 2, 3, 4)
10

This is a simple summation example. Unlike the static type of C/C ++, Python does not limit the type of parameters passed into the sum function:
Copy codeThe Code is as follows:
>>> Def printArs (* args ):
... For a in args:
... Print ()
...
>>> PrintArs (2, 3, [2, 2], (2,), 'df ')
2
3
[2, 2]
(2 ,)
Df
>>> PrintArs (* (2, 3, [2, 2], (2,), 'df '))
2
3
[2, 2]
(2 ,)
Df
>>> PrintArs (* [2, 3, [2, 2], (2,), 'df '])
2
3
[2, 2]
(2 ,)
Df

Here the args corresponds to the arguments of Javascript;

In addition to using tuple to receive variable parameters, you can also use dictionary to receive named parameters:
Copy codeThe Code is as follows:
>>> Def printArs (** args ):
... For k in args:
... Print (repr (k) + "=" + repr (args [k])
...
>>>
>>> PrintArs (name = 'wyj', age = 24)
'Name' = 'wyj'
'Age' = 24
>>> PrintArs (** {'name': 'wyj', 'age': 24 })
'Name' = 'wyj'
'Age' = 24

Of course, more complex, you can use * arg, * arg in combination. Default Value features:


Python user-defined functions,

The Rows starting with... are function/Type Definitions,
>>> The row at the beginning is the execution

Therefore, after defining a function, you need to enter an additional carriage return to end the definition.

How does a python UDF re-run itself? Is there such code?

I don't know what you mean by re-running yourself? Recursion? Or call?

Previously written, copy the folder, you can see, hope to help you!
Import OS
Import shutil
Def my_copytree (src, dst ):
Names = OS. listdir (src)
If not OS. path. exists (dst ):
OS. mkdir (dst)
For name in names:
Srcname = OS. path. join (src, name)
Dstname = OS. path. join (dst, name)

If OS. path. isdir (srcname ):
My_copytree (srcname, dstname) # recursively traverse folders
Else:
If (not OS. path. exists (dstname) or (OS. path. exists (dstname) and (OS. path. getsize (dstname )! = OS. path. getsize (srcname )))):
# Print dstname
Shutil. copy2 (srcname, dst)

If _ name _ = '_ main __':
Src = 'C: \ caselog'
Dst = 'C: \ bug'
My_copytree (src, dst) ---- called here

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.