Nested classes in Python (inner classes call method functions in external classes)

Source: Internet
Author: User

In the book for the version is the 3. X, but 2. X does not support direct invocation.

So, in python2.x, to invoke the method of an outer class in an inner class, you have to instantiate the subclass and then pass in the instance for invocation.

It took me two hours ah, the information did not find, oneself an idea debugging, really failed thirty or forty times, print more than n ~ ~ ~:)

Class Diagramfactory: @classmethod def make_diagram (class, Width, height): return class.diagram (width, height ) @classmethod def make_rectangle (Class, x, y, width, height, fill= "white", stroke= "Black"): retur        n Class.rectangle (x, y, width, height, fill, stroke) @classmethod def make_text (Class, x, y, text, fontsize=12):    return Class.text (x, Y, Text, fontsize) BLANK = "" CORNER = "+" horizontal = "-" VERTICAL = "|" Class Diagram:def __init__ (self, width, height): Self.superclass = Diagramfactory () self.wid th = width Self.height = Height Self.diagram = diagramfactory._create_rectangle (self.superclass,self . width, Self.height, Diagramfactory.blank) def add (self, component): For Y, row in enumerate (COMPONENT.R OWS): For X, char in Enumerate (row): Self.diagram[y + component.y][x + component.x] = ch Ar def save (self, filenAmeorfile): File = (None if isinstance (Filenameorfile, str) Else filenameorfile) Try:if file is None:file = open (Filenameorfile, "w") for row in Self.di Agram:print >>file, "". Join (Row) Finally:if isinstance (filenameorfile , str) and file are not None:file.close () class Rectangle:def __init__ (self, x, y, width, he            ight, fill, stroke): Self.superclass = diagramfactory () self.x = x self.y = y Self.rows = Diagramfactory._create_rectangle (self.superclass, width, height, diagramfactory.blank if fi ll = = "white" Else "%") class Text:def __init__ (self, x, y, Text, fontsize): self.x = x s Elf.y = y Self.rows = [List (text)] def _create_rectangle (self, width, height, fill): Rows = [[Fill f] Or _ in range (width)] foR _ in range (height)] to x in range (1, width-1): rows[0][x] = diagramfactory.horizontal ro WS[HEIGHT-1][X] = Diagramfactory.horizontal for y in range (1, height-1): rows[y][0] = diagramfactory . VERTICAL Rows[y][width-1] = diagramfactory.vertical for y, X in ((0, 0), (0, Width-1), (Height-1, 0), (height-1, width-1)): rows[y][x] = diagramfactory.corner return rows

The key code is as follows:

Self.superclass = Diagramfactory ()
Self.width = width
Self.height = height
Self.diagram = Diagramfactory._create_rectangle (self.superclass, Self.width, Self.height, Diagramfactory.blank)

。。。。

def _create_rectangle (self, width, height, fill):

。。。。。

Nested classes in Python (inner classes call method functions in external classes)

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.