Python\ class

Source: Internet
Author: User

1 Initial class

1 declaring a class (similar to declaring a function)

The definition format of the class

Class Name:

' class's document string '

Class Body

2 Create a class:

Class Data:

Pass

Idioms used in Python programming use singular words and capitalize the first letter of the class name

A class is a combination of data and a function, which is called a property of a class

Class Garen: #定义英雄盖伦的类, different players can use it to instantiate their own heroes;

The camp of camp= ' Demacia ' #所有玩家的英雄 (Galen) is Demacia;

Def attack (Self,enemy): #普通攻击技能, Enemy is the enemy;

Enemy.life_value-=self.aggressivity #根据自己的攻击力, attack the enemy and lose the enemy's health.

Role of Class 2 1: Attribute reference

Property reference (class name. Properties)

(1) The Data property (class name) of the reference class. Variable name

Print (Garen.camp) # Reference class's Data property, which is shared with all objects/instances

The output is:

Demacia

(2) The function attribute (class name) of the reference class.

Print (Garen.attack) #引用类的函数属性, this property also shares

The output is:

<function Garen.attack at 0x00000059ce8faf28>

(3) Property manipulation of classes

Garen.name= ' Garen1 ' #增加属性

Print (Garen.name) #查询属性

The output is:

Garen1

Del Garen.name #删除属性

Print (Garen.name)

The output is:

Attributeerror:type object ' Garen ' has no attribute ' name ' #报错

garen.camp= "AAAA" #修改属性

Print (Garen.camp)

The output is:

Aaaa

Role of Class 3 2: Instantiation (1) __init__ instantiation

The class name parentheses are instantiated, which automatically triggers the operation of the __INIT__ function, which can be used to customize each instance's own features.

Class Garen:

camp= ' Demacia '

def __init__ (self,nickname,aggressivity=58,life_value=455):

Self.nickname=nickname #为自己的盖伦起个别名;

Self.aggressivity=aggressivity #英雄都有自己的攻击力;

Self.life_value=life_value #英雄都有自己的生命值;

Def attack (Self,enemy):

Print ("Attack%s"% enemy)

Instantiation: Class name + parentheses

G1=garen (' Bush lun ')

#就是在执行Garen. __int__ (G1, ' Bush Lun '), then execute __init__ within the code g1.nickname= ' Bush lun ' etc

(2) Self function

The function of self is to automatically pass the object/instance itself to the first parameter of the __init__ when instantiated, and it can be any name, but it is universally acknowledged.

This automatic transfer mechanism is also reflected in the G1.attack call, followed by the introduction of

A: Where do the properties of the class we define are stored? There are two ways of viewing

Dir (class name): A list of names is isolated

The class name. __DICT__: A dictionary is found, key is a property name, value is a property value

Second: Special class properties

Class name The name of the __name__# class (string)

The document string for the class name. __doc__# class

Class name the first parent class of the __base__# class (Speaking of inheritance)

The class name is a tuple of all the parent classes of the __bases__# class (Speaking of inheritance)

Class name. Dictionary properties for the __dict__# class

Class name. The module where the __module__# class is defined

Class name the class that corresponds to the __class__# instance (in the modern class only)

Python\ class

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.