Special properties of the class /Special property of ClassIn Python, classes are defined by class, and classes can be instantiated into instances and invoked using an instance.The class also contains some common special properties.
Special class Properties
Generates a random numeric password of the specified lengthGenerates a random letter password of the specified lengthGenerates a mix of random numbers and letters of a specified length#encoding =utf-8Import RandomImport stringClass Password_generator:Password_time=0def __init__ (self,length):Self.length=lengthdef digital_password (self):Password_generator.password_time+=1S= ""For I in Range (self.length):S+=str (Random.randint (0,9))return s@classmethoddef letter_password (cls,length):Password_g
.__init__ (XXX)4, There is also a way to use the adorner, if you follow this method will not perform multiple init#Coding=utf8 fromFunctoolsImportWrapsdefSingleton (CLS):PrintCLS Instances={} @wraps (CLS)defgetinstance (*args, * *kw):ifCls not inchInstances:instances[cls]= CLS (*args, * *kw)returnInstances[cls]returnGetinstance@singletonclassMyClass (object): a= 1M1=MyClass () M2=MyClass ()PrintM1 isM2The Python singleton mode is controlled to initial
Class 2 in python, pythonClass (2) in python 6. Member fields of the class: common fields and static Fields
Eg:
Class Province (): country = 'China' # The static field is saved in the class. During execution, you can access def _
the methods of your parent class in subclasses:Instance:#!/usr/bin/python#-*-coding:utf-8-*-class Parent: # defines the parents class def myMethod (self): print ' calls the parent class method ' class child ( Parent
1, the program entrance, let main show out:Print (__name__) #__name___是模块中的隐藏字段, function name of the current module runif __name__ = = __main____MAIN__ ()#定义程序入口: In Python, the main function is hidden by default, and the main function, under the currently running module, defines the program entry to make the main function explicit #现出来Def __main__ ():。。。。。。。2. Object-oriented and process-oriented:Object-oriented: Use objects to accomplish tasks, do
class header.Four, operator overloadingKey concepts of overloading* Operator overloading allows the class to intercept regular Python operations.* Class can overload all Python expression operations.* Classes can be overloaded with printing, function calls, attribute point
If there is no __init__ method function in a class, the instance object created by the class name is empty and the tangent is not initialized; If there is this method function, it is usually the first method function of the class, a bit like a constructor in a language such as C + +.
Class Ca:
def __init__ (self,
can force some of the attributes that we think must be bound to be filled in when the instance is created. By defining a special __init__ method, when creating an instance, bind the attributes such as Name,score: Class Ren (): def __init__ (self,name,sex): Self.name = Name self.sex = Sex def hello (self): print (' Hello {0} '. Format (self.name)) test = ren (' yangling ', ' M ') Test.hello ( The results are printed as:Explain: 1. When passing para
A few days ago to do a project, meet a similar problem. The parent class is a common class, which is used in many sub-projects, and subclasses are used as a basic class in many parts of the project, but there are some class attributes in the original parent class (note that
In this article let's look at the knowledge about classes, some friends may have just come into contact with the Python programming language, for
Python class PropertiesThis is not a special understanding, but it's okay. The next article will come to take you to learn
Python Class
): "" defines a class method with at least one CLS parameter "" " print ' class method ' @staticmethod def static_func (): "" " defines a static method, no default parameter" "" print ' static method ' # call normal method F = Foo () F.ord_func () # Call the class method Foo.class_func () # Call the static method Foo.static_func
', ' __subclasshook__ ', ' __weakref__ ', ' hobbies ', ' infoma ', ' tall ' ]
3, static method and instance method and class method are different, static method has no parameter limit, need neither instance parameter, nor need class parameter, use @staticmethod adorner when defining. Like similar methods, static methods can be accessed through class names or th
The initialization method of the class in Python is __init__ (), so the initialization method of the parent class and subclass is this, the following article mainly introduces the example of the method of calling the parent class function of the Python neutron
)
If according to the above criteria, only use Mixin form of multiple inheritance, then there will not be a diamond inheritance to bring a duplicate method call, there will be no complex search order-at this time, super can have no, with no need to look at a person's liking, just remember never and the class name call of the way to mix. Sometime you think that the parent name is not good, change to myparent, the result has to change each subclass t
This article was reprinted from: Kaka_ace ' s blogWhen we develop with Python, we encounter the notation of Class A and Class A (object),This is a conceptual and functional distinction in Python2, the distinction between classic classes (old Class) and modern classes,Described in English as Old-style (Classic-style) an
)
The Mixin class should have a single responsibility (refer to the interface design of Java, Mixin is very similar to this, but only with implementation)
According to the above standard, only multi-inheritance in the Mixin form is used, so there will be no repeated method calls resulting from diamond inheritance, and there will be no complicated search order-at this time, super is available, you don't need to look at your personal preferences. You j
(self ): SchoolMember. tell (self) print 'Marks: "% d" '% self. marks t = Teacher ('Mrs. shrividya', 40,300 00) s = Student ('swaroop ', 22, 75) print # prints a blank line members = [t, s] for member in members: member. tell () # works for both Teachers and Students 4. Key Concepts of Operator Overloading * Operator Overloading blocks regular Python operations. * Class can overload all
1 Subclass Call Parent Class construction methodclassAnimal (object):def __init__(self):Print("Init Animal class~") defRun (self):Print("Animal run!")classDog (Animal):def __init__(self):#The Kawai class does not override the constructor method, and the parent class is called. Otherwise,
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.