Beginner Python class

Source: Internet
Author: User

While watching Python's official 2.7 document, try to test some examples to deepen your impressions.

Official Document URL: http://python.usyiyi.cn/python_278/tutorial/classes.html

The code is as follows:

#Coding:utf-8##################Practice Application Classes#################classTest:def __init__(self, name): Self.name=name x= Test (1)    PrintX.name#here try to output ' Hello ' ———— change line tenth to: x = Test (u ' hello ')###################classtestvarible:samething='Hello Word.'    #Shared Variables    def __init__(self, name): Self.name= Name#unique variables for each instance One= Testvarible (' One') the= Testvarible (' Both')Print 'One :', One.name,'; Same thing:', One.samethingPrint 'Both :', Two.name,'; Same thing:', Two.samething###################classTesterror:tricks= []#The right thing to do is to move this sentence in the __INIT__ function or in the Add_tricks function .#def __init__ (self):#                   defadd_tricks (Self,stuff): Self.tricks.append (stuff) Dog1=Testerror () dog1.add_tricks ('Dog1') dog2=Testerror () dog2.add_tricks ('dog2')PrintDog1.tricks,'\ n----------'PrintDog2.tricks#after the output you will find that tricks is a shared list and the object's data becomes shared.#It is important to note that the attribute name and the method name in the class must be distinguished from each other, and there will be a hard to find error.#declaring a naming convention again can avoid a lot of hassle. ####################

Beginner 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.