python - Classes, Hidden Attributes and Inheritance -


I'm fairly new to OOP and Python. Many things that I know are taught themselves, so I would be grateful if you could just give me the link to read because I do not have the exact terminology, which I should seek to clarify my doubts.

This is my code yet:

  square point (object): x = 2 y = 3 def __init __ (self, x, y): self.x_a = X self.y_b = Y class rectpoint (point): def __init __ (self, x, y): self X_1 = x self. Y_1 = y = self (x * x + y * y) ** 0.5 square circuit (point): pass a = point (3,4) b = circuit (3,4) c = racket point (3,4) print Ax # 2 Print Ai # 3 Print a.x_a # 3 Print a.y_b # 4 Print by Bx # 2 Print # 3 Print B.X_A # 3 Print B.I_B # 4 Print CX # 2 Print C # 3 Print C . X_1 # 3 Print CII # 4 print cdialist # 5.0 print cx_e # attribute er print ci_b # attribute error   

attributes self.x_a and self.y_b Not available for Class RectPoint but Circap is available for?

Forward, why are self.x and self.y available for two sections RectPoint <

< / Div>

"post-text" itemproc = "text">

your rectpoint __ init __ method mask to the dot .__ init __ method. Because dot .__ init __ has not been executed for the RectPoint examples, self.x_a and self.y_b attributes are never set. This is because Python first searches for the example, then the square, then the base class for the attributes. The methods are really special features, here. When creating an example of a class, then look at the __ init __ method on the Python class, then on the base class.

Circuit does not apply to themselves __init __ hence point .__ init __ is found and used , And self.x_a and self.y_b are set.

You do specialty class attributes for x and y ; The dragon finds these with the same search order; They are not set to the example, and eventually the points are found on the base class; Either because you have a subclass of dot example or dot and the base squares of that subclass were searched.

You obviously code overidade __init __ method to RectPoint .__ init___ : < Pre> class RectPoint (dot): def __init __ (self, x, y): super (rectpoint, self) .__ init __ (x, y) self. X_1 = x self. Y_1 = y self-dest = (x * x + y * y) ** 0.5

lets you enter the original dot .__ init __ method here Call it first. This method then sets the self.x_a and self.y_b attributes, after which it will return and the RectPoint .__ init __ method Features will set.

Includes everything in the description.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -