Python Basics-exceptions, objects, and iterators

Source: Internet
Author: User

    • Exception handling
    • Object oriented
    • Iterators and generators
Python Exception handling
    • The following code triggers a filenotfounderror
>>> Open ("Notexist.txt") Traceback (most recent call last):  File "<stdin>", line 1, in <module> Filenotfounderror: [Errno 2] No such file or directory: ' Notexist.txt '
    • Throw exception
>>> Raise Filenotfounderror
    • Exception type

    • Catching exceptions using Try,except,finally,else
Try:  open (r "C:\Users\kingsoft\Desktop\notexist.txt") except Filenotfounderror as E:  print ("File not exist ... ") except (name1,name2):  print (" IO error is true ... ") Else:  print (" file exist.. ") Finally:  print ("Always do ...")

  

Python Object-oriented

Python is completely object-oriented, and everything in Python is an object, including variables, functions, and so on.

    • Defining an Object
Class MyException ():p
    • Self and Init methods
Class person ():d EF __init__ (Self, name): Self.name = Namedef sayname (self):p rint Self.namem = person ("Joe") print ( M.sayname ())
    • Examples of district classifications and classes
Class person ():d EF __init__ (Self, name): Self.name = Nameperson.name = Namedef sayname (self):p rint ("MyName is:" + Self.nam e) Print ("Myexceptionname is:" + person.name) def changeothername (self, name):P erson.name = Namedef __del__ (self):p rint ( Self.name + "is Gone") m = Person ("Joe") M.sayname () print ("M.name:" + m.name) m.test= "TT" Print (m.test) j = Person ("Jason") J . Sayname () j.changeothername (j.name) m.sayname ()
    • Class inheritance, polymorphism, and encapsulation concepts
Generators and iterators
    • The _iter_ method returns an iterator that refers to an object with the next method
Class Fibs (object): "" "DocString for Fibs" "" "Def __init__ (Self, max): Self.max = MAXSELF.A = 0self.b = 1def __next__ (self): fi b = Self.aif fib > self.max:raise stopiterationself.a, self.b = self.b, SELF.A + Self.breturn fib# return iterator def __iter__ (sel f): Return SELFFIB = Fibs (+) for F in Fib:print (F, end= "")
    • Object Iterable and iterators can be iterated iterator
    • Generator, the generator quickly generates iterators through yield statements, making the function a generator
#斐波那契数列def getfibs (max): a = 0b = 1while A < Max:a, b= b, a+bvalue = Ayield Valueprint (getfibs (+)) for I in Getfibs (10 XX):p rint (i)
    • Simple Understanding Generator
Def gen (): Yield "Hello" yield "how" yield "is" yield "you" for I in Gen ():p rint (i)

  

Python Basics-exceptions, objects, and iterators

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.