Creation and assignment of Python objects

Source: Internet
Author: User
Tags assert naming convention

Create Class

The definition of a class begins with the keyword class, followed by a name (user-defined) to identify the class, and ends with a colon. The contents of the class are indented (4 spaces), and the following example pass indicates that nothing is done.

The Python naming convention (preceded by a letter or underscore, the name can contain only letters, underscores, or numbers). The camel name is typically used (starting with a capital letter, and any word that follows immediately begins with a capital letter).

class Myfirstclass:     Pass

Save the above code as a first_class.py file, and then run the command python-i first_class.py. The parameter-I representation runs the script in the interactive interpreter.

- I a = myfirstclass ()  Print (a)<__main__. Myfirstclass object at 0x103fd7e80>>>> B = myfirstclass ()print(b)<_ _main__. Myfirstclass Object at 0x103fd7eb8>

Two objects A and B have been instantiated. Type the name of the class followed by a pair of parentheses.

Add Property

It is possible to assign arbitrary attributes to an instantiated object by point notation, in the form of <object>.< attribute> = <value> To assign a value to a property, which can be arbitrary, such as Python's built-in data type, other objects, or even a function or another class.

class Point :     Pass  = = = 5 = 4 = 3 = 6 Print(p1.x, p1.y)   Print(p2.x, P2.Y)

This code creates an empty point class without any data and behavior. It then creates two instances of the point class and assigns each instance an x-coordinate and a y-coordinate. Running the code above will result in the following.

5 4 3 6
Let the class actually do something

Above, we can have an object with attributes, and then, by firing some behavior to cause these properties to change. A reset method is added to the point class, which is a good example of moving points to the origin (the method does not need to use parameters).

 class   point:  def   reset (self): self.x  = 0 SELF.Y  = 0p  = point () p.x  = 5p.y  = 4print   (p.x, P.Y)  #   5 4  p.reset ()  print   (p.x, p.y)  #  0 0  

Method definitions in Python are basically the same as functions (function), and the method can manipulate the object itself and any arguments passed in. The method differs from the function in that all methods have a required parameter , which is often referred to as self, and if forgotten, an error occurs when the method is referenced. The self parameter in a method is a reference to the object that invokes the method, and we can access the properties and methods of the object like any other object. When you want to change the value of the X and Y properties of the Self object, it is implemented by calling the internal reset method.

When calling P.reset () and not passing it the self parameter, Python automatically passes the object to the method .

The method is just a function, except that it happens to be in a class. In addition to the method of invoking an object directly, we can call the function in the class and explicitly pass the object to the object as the self parameter.

class Point :     def Reset (self):         = 0        =point ()Point.reset (p)print(p.x, p.y)#  0 0

How do I pass multiple parameters? Suppose you add a new method to the point class, you can move the points to any location, not just the origin. You can also accept another point object as input, and then return the distance between the two objects.

ImportMathclassPoint :defMove (self, x, y):#accepts x and y two parameters and assigns a value to the Self objectself.x =x self.y=ydefReset (self):#move to a specific locationself.move (0, 0)#accepts another point object as input, returns the distance    defcalculate_distance (Self, other_point):returnmath.sqrt ((self.x-Other_point.x) * * 2 +(Self.y-OTHER_POINT.Y) * * 2)#How to use itPoint1 =Point () Point2=Point () Point1.reset () Point2.move (5, 0)Print(Point2.calculate_distance (point1))#5.0#assert is a simple test tool, and if the following statement is False (0, empty, or none), the program exits unexpectedlyassert(Point2.calculate_distance (point1) = =point1.calculate_distance (Point2)) Point1.move (3, 4)Print(Point1.calculate_distance (point2))#4.472135955Print(Point1.calculate_distance (point1))#0.0

Reference:

1, "Python3 Object-Oriented Programming" [Plus]dusty Philips

Creation and assignment of Python objects

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.