1. Function calls
A = []
def fun (a):
A.append (1)
Print (a)
This time the result of the output is [], if you want to appear 1, you need to run the function fun first, and then in the output a
2.python method
def demo1 (x):
Print ("My is one demo" +x)
Class A (object):
def demo2 (self,x):
Print ("aaaaaa")
@classmethod
def class_demo2 (cls,x):
Print ("BBBBBB")
@staticmethod
def static_demo2 (x):
Print ("CCCCCC")
A = A ()
A.demo2 (2)
A.class_demo2 (3)
A.static_demo2 (4)
A.class_demo2 (4)
A.static_demo2 (6)
For function arguments self and CLS are actually bindings to classes or instances, for general functions, we use demo (x) directly to invoke, and its work is independent of class or instance. For instance methods, you need to bind this instance every time the class defines a method, that is, Demo2 (self,x), why do you do this? Because the invocation of the instance method is inseparable from the instance, we need to pass the instance to the function, which is called when the A.demo2 (x) is in fact (Demo2 (a,x)), and the class method is the same, except that it passes the class instead of the instance, A.class_demo2 (x). Static methods, like normal methods, do not need to be bound to anyone, and the only way to cry is to invoke it using A.static_demo2 (x) or A.static_demo2 (x).
3. Class variables and instance variables
Name = "AAA"
P1 = person ()
P2 = person ()
P1.name = "BBB"
Print (P1.name)
Print (P2.name)
Print (Person.name)
The class variable is the variable that is used by the class, the instance variable is for the instance to use, p1.name= "BBB", is the instance invokes the class variable, but in the scope of the instance changes the class variable reference, becomes an instance variable.
4. Several methods
Isinstance ()
Fixes duplicate types in the second argument of isinstance()
. For example, was isinstance(x, (int, int))
converted to isinstance(x, (int))
.
Hasattr ()
hasattr
(object, name)
The arguments is an object and a string. True
The result is if the string is the name of the one of the object ' s attributes, and False
if not. (This was implemented by calling and getattr(object, name)
seeing whether it raises a AttributeError
or not.)
GetAttr ()
-
GetAttr
(
object ,
name [,
default ])
-
Return The value of the named attribute of
object .
name must is a string. The If the string is the name of the one of the object ' s attributes, and the result is the value of this attribute. For example,
getattr (x, ' Foobar ')
is equivalent to
x.foobar
. If the named attribute does not exist,
default is returned if provided, otherwise
attributeerror
is raised.
-
5. Underline
Class Myclass ():
def __init__ (self):
Self.__superprivate = "Hello"
Self._semiprivate = ", World"
MC = Myclass ()
Print (mc._semiprivate)
Print (mc.__dict__)
__DEMO__: A convention, a name inside Python that distinguishes other custom names from conflicts
__demo: A convention that specifies a way to specify a private variable
6. String formatting:% and Format
Name = LSS
"Ni hao%s"%name
But if name=, it must be written like this: "Ni hao%s"% (name,)
Python small clutter Point