It's not long enough to touch the Python language, and many of the features of the language are not well understood, and many usages are not yet known. Today I want to write a function call in the inheritance of Python object-oriented programming. Share it and make progress together.Because of previous exposure to Java and C + +, all of the ideas for object-oriented are already well-known. There is no longe
variables and cannot access instance variables.
When we directly access the initialized instance variable, we are prompted without this attribute (Attributeerror:type object ' ClassName ' has no attribute ' arg ').
1 classClassName (object):2 """docstring for ClassName"""3 4Name ='Pig' #This is a class property5 6 def __init__(Self, arg):#Init initializes the instance properties and instan
How to obtain the list of class attributes in Python, and obtain class attributes in python
Preface
In my recent work, I have encountered a need to obtain a static attribute of the class, that is, there is a class Type, and I want
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
.
When a reference is passed to a function, the function automatically copies a reference, and the reference in the function does not have a semi-gross relationship with the outside reference. So the first example is that the function refers to an immutable object number, and when the function returns, the outside reference does not feel half-hairy. And the second example is different, The reference in the function refers to the Mutable object list, which points to the memory address of the
The use and difference of class method, class instance method, static method in Python2011-03-24 11:58:06| Category: Python | Tags: Python class method static method | report | Font size SubscriptionTransfer from http://stonelee.javaeye.com/blog/726449How to use:
Note: The Python2.7 is used.
One, instance method
An instance method is a method that an instance of a class can use. As follows:
Copy Code code as follows:
Class Foo:
def __init__ (self, name):
Self.name = Name
def hi (self):
Print Self.name
if __name__ = = ' __main__ ':foo01 = Foo (' Letian ')Foo01.hi ()Print type (Foo)Print type (FOO01)Print ID (foo01)Print ID (Foo)
The
Attributes are divided into instance properties and class propertiesMethods are divided into ordinary instance methods, class methods, static methodsBoth static and class methods of Python can be accessed by a class or instance, and the difference is obvious:1) Static method
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
#-*-Coding:utf-8-*-# python:2.x__author__ = ' Administrator '#元编程#new-style class provides the ability to modify the definition of classes and objects at run time through 2 special methods (__NEW__ and __metaclass__)#__new__方法#它是一上元构建程序, it is called every time an object is instantiated by the factor class#例如:Class A (
interface
Realize
Implementing relationships refers to the process by which a class implements one or more interface functions, where the interface is more of a contract or specification. Implementations are one of the most coupling relationships between two classes or between classes and interfaces, where classes implement the actions declared in an interface or interface class. UML us
): "" 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 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.