Python\ Object (instance)

Source: Internet
Author: User

1. The object is an example of a class that actually exists, i.e. the instance

#类实例化得到g1这个实例

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)

G1=garen (' Bush lun ')

1.1 Property references and binding methods for objects

(1) An object (instance) has only one function: Property reference

Format: Instance name the variable name of the class.

Instance Name. Binding method

Instance name. Instance of its own variable name

Print (G1.nickname)

Print (g1.aggressivity)

Print (G1.life_value)

The output is:

Bush LUN

58

455

(2) Property manipulation of objects

View property Information

Print (G1.nickname)

Output Result:

Bush LUN

modifying property information

G1.nickname= "Brother Lun"

Print (G1.nickname)

The output is:

Brother Lun

Add Property

g1.sex= "Female"

Print (G1.sex)

The output is:

Female

To delete a property:

Del G1.sex

Print (G1.sex)

The output is:

Attributeerror: ' Garen ' object has no attribute ' sex ' #报错

(3) View instance properties

The same dir and built-in __dict__ two different ways

Special Instance Properties

__class__

__dict__

(4) How to bind an object (instance)

The object itself has only data properties, but the Python class mechanism binds the function of the class to the object, called the object's method, or is called a binding method.

Print (G1.attack) #对象的绑定方法

Print (Garen.attack) #对象的绑定方法attack本质就是调用类的函数attack的功能, which is a binding relationship

The output is:

<bound method Garen.attack of <__main__. Garen Object at 0x00000017370815f8>>

<function Garen.attack at 0x0000001737085048>

The special thing about object binding methods is that Obj.func () passes obj to the first argument of Func

1.2 Interaction of objects

Create a riven class modeled after the Garen class:

Instance Riven class

Interaction: Rui Wen attack Bush Lun

Class Riven:

camp= ' Noxus '

def __init__ (self,nickname,aggressivity=54,life_value=414):

Self.nickname=nickname #为自己的瑞雯起个别名;

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

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

Def attack (Self,enemy):

Print ("Attack%s"% enemy)

Enemy.life_value-= self.aggressivity

G1=garen (' Bush lun ')

R1=riven (' Rui Wen ')

Print (G1.life_value)

R1.attack (G1)

Print (G1.life_value)

The output is:

455

Rui Wen attack Bush Lun

401

Python\ Object (instance)

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.