Python classes, objects, methods, properties

Source: Internet
Author: User

In python , the characteristics of an object are also known as attributes (attribute). The behavior it has is also called the method.

Conclusion: Object = attribute + method

In python , classify objects with the same properties and methods as one class

such as humans, animals, plants, etc., these are the concepts of the class.

A class is a template or blueprint for an object, a class is an abstraction of an object, and an object is an instantiation of a class. A class does not represent a specific thing, but an object represents something specific.

>>> class People:

... def speak (self):

... print ("hello!")

...
"' defines a people class that defines a speak method, but does not define a property because the property is not part of a class, but rather belongs to an instance of each class. That is, belongs to the object. So we can set different properties for each instance.

>>> class People: #类

... def speak (self): #方法

... print ("hello!")

...

>>>
>>> Jack = people () #创建jack实例

>>> Tom = people () #创建tom实例

>>> Import Tab #导入table键功能模块

>>> Jack. #输入jack. <tab key, you can see the following methods

jack.__class__ jack.__doc__ jack.__module__ Jack.speak ()

>>> jack.speak () #引用speak方法 hello!
>>> jack.age=39 #添加age属性

>>> jack.height=120 #添加height属性

>>> Jack. jack.__class__ jack.__module__ jack.height jack.__doc__ jack.age jack.speak ()

>>> Jack.height 120

>>> Jack.age 39
‘‘‘

#初始化对象 you create a class, you can define a specific method, named __init__ (), that runs this method whenever an instance of the class is created.

You can pass parameters to the __init__ () method so that when you create the object you can set the property to the value you want __init__ () This method will complete initialization when the object is created.
‘‘‘

>>> class PEO:

... def __init__ (self,name,age,sex):

.. self. Name = Name

.. self. Age = Age

.. self. sex = Sex

... def speak (self):

... print "My name" + self. Name

...

>>> when instantiating objects of this class:

>>> Zhangsan=peo ("Zhangsan", +, ' man ')

>>> print Zhangsan. Age 24

>>> print Zhangsan. Name Zhangsan

>>> print Zhangsan. Sex Man #----------

>>> Print Zhangsan < __main__.peo instance at 0x7fe5041ec248>

‘‘‘

To print it out, you have to use the __str__ method __str__ () This method tells Python exactly what to display when printing an object (print)

‘‘‘

#! /usr/bin/python class PEO:

def __init__ (self,name,age,sex):

Self. Name = Name

Self. Age = Age

Self. sex = Sex

def speak (self):

Print "My name" + self. Name

def __str__ (self):

Msg= ' My name is: ' +self. Name+ "," + "My Age was:" + self. Age + ', ' + ' My sex is: ' +self. Sex

Return msg

SHANGHAI=PEO (' Shanghai ', ' All ', ' man ')

' msg= ' My name is: ' +self. Name+ "," + "My Age was:" + self. Age + ', ' + ' My sex is: ' +self. Sex

Here 23 is age, but is turned into a string because self. Age defines a string if you do not escape 23, you will get an error if you want to escape in the program beforehand, you need to use STR (self. Age) '

Print Shanghai

The parameter "Self" was used many times before

A class is like a blueprint, using a class to create multiple object instances, and the Speak () method, when called, must know which object called it.

The self parameter here tells the method which object to invoke. This is called an instance reference. Zhangsan.speak () is like writing a peo.speak (Zhangsan) "

__author__ = Kayson

Python classes, objects, methods, properties

Related Article

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.