Python Learning notes (v)

Source: Internet
Author: User
Tags timedelta

38. Inheritance

when a subclass inherits from a parent class, it does not write an init method that inherits the parent class . If the init method is written, the init method that overrides the parent class is overridden . The same is true of other methods. The same is true for variables.

Class A:

def __init__ (self):

Self.a= "a"

Self.b= "B"

def print (self):

Print (SELF.A)

Class B (A):

def __init__ (self):

Super (B,self). __init__ () // Note the format of the inherited writing, the class name writes its own class, self cannot be missing.

Self.c= "C"

def print_b (self):

Print (SELF.B)

39. Date Arithmetic

Import Datetime,time

Calculate the difference between two dates:

Date5 = Datetime.datetime.strptime (' 2012-9-22 12:35:40 ','%y-%m-%d%h:%m:%s ')
Date6 = Datetime.datetime.strptime (' 2015-9-22 10:35:40 ','%y-%m-%d%h:%m:%s ')

Sep = date6-date5

Print (Sep)

Calculate the date after three days:

now = Datetime.datetime.now ()
Delta = Datetime.timedelta (days=3)
N_days = Now + Delta
Print (N_days.strftime ('%y-%m-%d%h:%m:%s '))

where the type of Strptime is the time class, not the string format.

date1 = Time.ctime () # is a string and cannot be added or reduced

date2 = Datetime.date.today () # is a date class that can be operated with Timedelta

date3 = Datetime.datetime.now () # is a datetime class that can be operated with Timedelta

Although it is possible to perform operations with Timedelta, no operations can be performed between classes, such as date2-date3.

time.strptime (), Datetime.datetime.strptime () , both convert a string to a time class, but belong to a different class.

to enter a time into a start input box and an end input box, you need to convert to a string type:

StartTime = Datetime.date.today ()

Starttimestr = Starttime.strftime ()

EndTime = Starttime-datetime.tiemdelta (days=3)

Endtimestr = Endtime.strftime ()

40. Log Module

Import logging

Logging.basicconfig (filename="Config.log", filemode="a",
format="% (asctime) s--% (name) s--% (levelname) s:% (message) S", level= Logging.info)

console = logging. Streamhandler ()
Logging.getlogger (). AddHandler (console)

# The above two lines are output to the console at the same time, convenient debugging.
Logging.error ("This is a error log.")
Logging.info ("This is a info log.")

41. Private variables in python

(1) _xx, a variable of type protected that begins with a single underscore . That is, the protection type can only allow access to its own and subclasses. If an internal variable is identified as, an object that starts with an underscore will not be introduced when using the from M import * .

(2) __xx, a double underline represents a variable of a private type. Only the class itself can be accessed, and the subclass cannot be used to name a class attribute (class variable), and the name is changed when called (Within the class FooBar ,__boo becomes _ Foobar__boo, such as self._foobar__boo)

(3) __xx__ is a special method of definition. Variables or attributes within a user-controlled namespace, such as __init__,__import__ or __file__. Do not define such variables yourself unless the document is described. ( that is, these are the variable names defined within python )

The private variables are highlighted here,and Python's default member functions and member variables are public, and there are no publicly-available languages like others ,private such as keyword decoration, but you can precede the variable with two underscore "__", so that the function or variable becomes private, this is The existing variables of Python rolling (the translation is very awkward, the English name is Private name mangling)

42. list expressions and List Builder

A list derivation consists of parentheses consisting of an expression followed by a for clause, followed by 0 or more for or if words, the result is a list, The result of an expression based on the context of the for or if clause that follows it

A = [(x, Y) for x in [All-in-A-z] for y in [2,3,5] if x!=y]

in this way,a creates a list, with each element being a tuple of xandy .

the builder expression resembles a list expression: m = ((x, y) for x in [All-in-A-z] for y in [2,3,5])

Reference the value in the builder expression:

Print (m.__next__ ())

Print (m.__next__ ())

Summary: (1) The expression of the list [] is replaced by () is the generator expressions; (2 List parsing and builder expressions are a handy way of programming, except that generator expressions are more memory-efficient.

The elements in the list are weighed down

the list has methods for deleting elements, such as list.pop (i),list.remove (value),del list[i]. It is important to note that after each element is deleted, the subsequent element will automatically move forward one bit, resulting in the use of the for traversal, each delete one, followed by an element subscript automatically minus 1, so that the element is not traversed, Thus the omission occurred.

To Delete all the repeating elements in the list, you cannot simply use for, but recursively. as follows:

List1 = [1,2,3,3,3,2,2,5,6,5,6]
def deldupli (list1):
for i in List1:
if list1.count (i)! = 1:
List1.remove (i)
Deldupli (List1)
Deldupli (List1)
Print (List1)

Python Learning notes (v)

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.