This article mainly introduces the usage examples of Python _ setattr _, _ getattr _, _ delattr _, and _ call, this article explains these magic methods separately. For more information, see
Getattr
The 'getattr' function is a built-in function and can be obtained by function name.
The code is as follows:
Value = obj. attributeValue = getattr (obj, "attribute ")
Use 'getattr 'to implement the factory mode
to use Os.path.join (Dirpath, name). Auto-complete recursive enumeration via for loop for example: F:\aaa directory is such a file directory structure f:\aaa|--------1.txt| --------2.txt|--------3.txt|--------4 |-------5.txt |-------6.txt |-------7.txt Os.walk (Top, Topdown=true, Onerror=none, Followlinks=false) can get a ternary tupple (Dirpath, Dirnames, filenames), the first one for the starting path, The second is the folder under the starting path, and the third one is the file under the s
Examples of how to add and compare the combined values of the dictionary in Python.
Sum of dictionary merge valuesWhen collecting and summarizing game data, some data is stored in dictionaries every day. When I want to collect statistics for multiple days, I need to merge the dictionaries.If the keys are the same, their values are added.The update method cannot be used, because the value of the same key is
-dimensional list becomes a two-dimensional list, the two list changes at the same timeThis is a shallow copy, cloning only the first layer listThe second list is a reference to the first listBased on the shallow copy, modify the copy out of the list, you can implement the concept of similar inheritanceDeep copyImport CopyCopy.copy (a_list): shallow copy, same as A_list.copy ()Copy.deepcopy (a_list): Deep copyDon't use it. Large amount of data memory consumptionTwo-dimensional listEach element i
.insert (1, ' abc ')SortList2.sort ()ReverseList2.reverse ()Delete and return a valueList.pop (1)Append elementList2.extend (' Qweq ')Tuple ()Tuples and lists are similarTuples and strings are not becoming-tuples can store a range of values-tuples are typically used in user-defined functions to safely take a set of worthwhile times, that is, the value of the tuple being used does not change.T= (1,a,)Split of a B c=t tupleCommon methods of dictionaries> Dictionary is the only type of mapping (has
This article mainly introduces the usage examples of super in Python, this article compares the general inheritance with the super inheritance, from the results of the run, ordinary inheritance and super inheritance are the same, but in fact their internal operating mechanism is different, this point in multiple inheritance is evident, Need friends can refer to the following
Super is used to solve multiple
Careful use of variable length parametersPython supports variable-length parameter lists, which can be implemented by *arg, **kwargs these two special syntaxes. The following examples are used for variable length parameters:* Use *args to implement the variable parameter list: *args with AcceptA parameter list wrapped as a tuple to pass a non-critical parameter, and the number of parameters can be arbitrary.
def sumfun (*args): Result
= 0
for
Examples and differences of using exit, return, sys. exit () in Python
This article mainly introduces the use cases and differences of exit, return, sys. exit () and so on in Python. This article is a summary of a practical project. For more information, see
There is a question: String identifier. modify the idcheck of Example 6-1. py script to detect the identi
.
Struct. UnpackStruct. Unpack is the opposite of struct. Pack. It is used to replace byte Transfer with Python data type. Its function prototype is struct. Unpack (FMT, string). This function returns a tuples. The following is a simple example: Str = struct. Pack ("II", 20,400)A1, a2 = struct. Unpack ("II", STR)Print 'a1: ', A1Print 'a2: ', A2# ---- Result:# A1: 20# A2: 400STR = struct. Pack ("II", 20,400)A1, a2 = struct. Unpack ("II", STR)Print 'a1
, etc. Examples of how to use Env.roledefs:Env.roledefs = {' webserver ': [' 192.168.1.21 ', ' 192.168.1.22 '], ' dbserver ': [' 192.168.1.25 ', ' 192.168.1.26 ']}# A python adorner is used to refer to a grouping, such as: @roles (' webserver ') def webtask (): Run ('/usr/local/nginx/sbin/nginx ') @roles (' webserver ', ' DBServer ') def publictask (): Run (' uptime ')Fabric Common APIsLocal #执行本地命令, such a
value of the variable K, and the second%s is replaced by the value of V. All other characters of the Python format string (in this case, the equals sign) are printed out as-is. Note (k, v) is a tuple. String formatting is more than just a connection. It's not even just formatting. It is also a coercion type conversion. String Formatting vs. String joins >>> uid = "sa" >>> pwd = "secret" >>> print pwd + "is not a good password for" + UID Secret is n
Before we talked about the Python advanced data Structure Learning tutorial, let's take a look at some of the advanced Python design structures and how they are used. in daily work, you can choose the right data structure for your needs, such as requirements for fast lookup, requirements for data consistency, or requirements for indexing, and you can combine various data structures appropriately to gener
This paper describes the use of Optionparser module in Python in detail, and it has good reference value for studying python in depth. Share for everyone to use for reference. The specific analysis is as follows:
In general, there are two built-in modules in Python that handle command-line arguments:
One is getopt, "Deep in
This article mainly introduces examples of Python smtplib module to handle the use of e-mail, is the basics of Python introductory learning, need friends can refer to the
In internet-based applications, programs often need to send e-mail automatically. For example: A website registration system will send a message to confirm registration when the user registers;
This article mainly introduces the differences between Python and C programming ideas through examples. as a representative of object-oriented and process-oriented programming languages, the comparison between the two is classic, for more information about how to use Python to process various data science projects, see. Pytho
This article mainly introduces the use of continue statements in Python examples, is the basic knowledge of Python introduction, the need for friends can refer to the
The Python continue statement returns the start of the while loop. The continue statement rejects the rest of the statements in the current iteration of
This paper describes the use of Python functions in detail in the form of examples, which is of great value to beginners ' friends in Python. Share for everyone to use for reference. The specific analysis is as follows:
In general, the Python function is written by a new statement that DEF,DEF is an executable stateme
The seven examples of Python regular expressions are used as a concept. Regular expressions are not unique to Python. However, regular expressions in Python are slightly different in actual use.
This article is part of a series of articles about Python regular expressions.
treated as False? And which is True?
* * In Python, none, 0 in any numeric type, empty string "", Empty tuple (), empty list [], empty dictionary {} are treated as false, and custom types, if the nonzero () or len () method is implemented and the method returns 0 or FALSE, its instance is also treated as false, and the other objects are true. **
Here's the simplest logical operation:
True and True ==> true true or True ==>
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.