Python path, day7-object-oriented into

Source: Internet
Author: User

The content of this article:

Object-oriented, class methods, property methods

Special methods for classes

Reflection

Exception handling

Socket Development Basics

First, the object-oriented Advanced Syntax section

static method:

# @staticmethod just nominally classified management, actually not related to the class

class Dog (object):      def __init__ (self,name):         = name     # turns the Eat method into a static method    def  eat (self        ):Print ("%s is eating" % self.name)   = Dog ("user  ") d.eat ()

The above call will have the following error, saying that eat needs a self parameter, but the call is not passed, yes, when the eat becomes a static method, and then through the instance call will not automatically pass the instance itself as a parameter passed to the auto.

1. when invoked, the instance itself is passed to the Eat method, i.e. D.eat (d)

2. remove the self parameter in the Eat method, but this also means that you cannot pass self in eat. Calls to other variables in the instance

class Dog (object):     def __init__ (self,name):         = name    @staticmethod    def  Eat ():        print(" is Eating"= Dog ("user") d.eat ()

Ii. Types of methods

# @classmethod

Only class variables can be accessed and instance variables cannot be accessed

classDog (object): Name="class Variables"    def __init__(self,name): Self.name=name @classmethoddefEat (self):Print("%s is eating"%self.name) d= Dog ("User") d.eat ()#Execution Result:class Variables isEating

Third, attribute method

# @proprty turn a method into a static property
# @eat. Setter change Properties
# @eat. Deleter Delete Properties
class Dog (object):      def __init__ (self,name):         = name     @property    def  Eat (self):        print("  %s Is eating" %self.name)  = Dog ("user") d.eat  # output: is eating

Column:

1 classFlight (object):2     def __init__(self,name):3Self.flight_name =name4 5 6     defChecking_status (self):7         Print("checking flight%s status"%self.flight_name)8         return19 Ten @property One     defFlight_status (self): AStatus =self.checking_status () -         ifStatus = =0: -             Print("flight got canceled ...") the         elifStatus = = 1 : -             Print("flight is arrived ...") -         elifStatus = = 2: -             Print("flight has departured already ...") +         Else: -             Print("cannot confirm the flight status...,please check later") +  A  atf = Flight ("CA980") -F.flight_status

Column 2: @proerty. Setter decorator to decorate again, you need to write a new method to change this flight_status.

classFlight (object):def __init__(self,name): Self.flight_name=namedefChecking_status (self):Print("checking flight%s status"%self.flight_name)return1@propertydefFlight_status (self): status=self.checking_status ()ifStatus = =0:Print("flight got canceled ...")        elifStatus = = 1 :            Print("flight is arrived ...")        elifStatus = = 2:            Print("flight has departured already ...")        Else:            Print("cannot confirm the flight status...,please check later") @flight_status. Setter#Modify    defFlight_status (self,status): Status_dic= {: "canceled",:"arrived",: "departured"        }        Print("\033[31;1mhas changed the flight status to \033[0m", Status_dic.get (status)) @flight_status. Deleter#Delete    defFlight_status (self):Print("status got removed ...") F= Flight ("CA980") F.flight_statusf.flight_status= 2#Trigger @flight_status.setterdelF.flight_status#Trigger @flight_status.deleter

Note that the above code also writes a @flight_status.deleter, which allows this property to be deleted

IV: Special members of the class

Python path, day7-object-oriented into

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.