Fully understand the classes objects methods and properties in python

  • 2020-05-10 18:23:08
  • OfStack

The 1 in python is all an object, the so-called object: I am an object myself, the computer I play is an object, the chair I sit on is an object, and the dog I keep at home is also an object...

We describe the properties (characteristics) and behavior of an object. For example, the dog at home, its color, size, age, weight and so on are its attributes or characteristics. It can bark, it can wag its tail and so on.

When we describe a real object (object), we include two aspects:

What it can do (behavior)

What it looks like (an attribute or a feature).

In python, the characteristics of an object are also called properties (attribute). The behavior it has is also called a method (method)

Conclusion: object = attribute + method

In python, group objects with the same properties and methods into one class (class)

Such as human beings, animals, plants and so on, these are all concepts of class.

The class is the template or blueprint for the object, the class is the abstraction of the object, and the object is the instantiation of the class. A class does not represent a concrete thing, but an object represents a concrete thing.


>>> class people: 

...   def speak(self): 

...       print ("hello!") 

...

'''

Define 1 people class, define 1 speak method, but do not define properties,

Because the attribute does not belong to the class, but to the instance of the individual class. In other words, belonging to an object.

So we can set different properties for each instance


'''  
>>> class people:          # class   
...   def speak(self):      # methods         
...       print ("hello!")        
... 
>>> 

>>> jack = people()  # create jack The instance 
>>> tom = people()  # create tom The instance 
>>> import tab    # The import table Key function module 
>>> jack.      # The input jack. , you can see the following 
jack.__class__  jack.__doc__   jack.__module__ jack.speak(   
>>> jack.speak()  # reference speak methods 
hello!

>>> jack.age=39      # add age attribute 
>>> jack.height=120    # add height attribute 
>>> jack.
jack.__class__  jack.__module__ jack.height   
jack.__doc__   jack.age     jack.speak(   
>>> jack.height
120
>>> jack.age
39

'''

# initializes the object

When creating a class, you can define one specific method, called s 41en__ (), by creating one instance of the class

I'm going to run this method. You can pass the parameters to the method s s 44en__ (),

This allows you to set the properties to the values you want when you create the object

This method will complete the initialization while creating the object,


'''
>>> class peo:
...   def __init__(self,name,age,sex):
...       self.Name = name
...       self.Age = age
...       self.Sex = sex
...   def speak(self):
...       print "my name" + self.Name
... 
>>> 

 Instantiate the object of this class: 
>>> zhangsan=peo("zhangsan",24,'man')
>>> print zhangsan.Age
24
>>> print zhangsan.Name
zhangsan
>>> print zhangsan.Sex
man

# ----------
>>> print zhangsan
<__main__.peo instance at 0x7fe5041ec248>
''' 

To get print to print, you have to use the method of s 57en__

This method tells python what to display when printing (print)1 object


'''
#! /usr/bin/python
class peo:
  def __init__(self,name,age,sex):
    self.Name = name
    self.Age = age
    self.Sex = sex
  def speak(self):
    print "my name" + self.Name
  def __str__(self):
    msg='my name is: ' +self.Name+ ","+ "my age is: " + self.Age +','+ "my sex is:" +self.Sex
    # msg='my name is: ' +self.Name+ ","+ "my age is: " + str(self.Age) +','+ "my sex is:" +self.Sex
    return msg
shanghai=peo('shanghai','23','man')
# shanghai=peo('shanghai',23,'man')
'''
msg='my name is: ' +self.Name+ ","+ "my age is: " + self.Age +','+ "my sex is:" +self.Sex

Here 23 is the age, but is converted to a string, because self.Age defines a string

If 23 is not escaped, an error is reported

If you want to escape in the program in advance, you need to use str(self.Age)

'''
print shanghai

'''
self has been used many times before
A class is like a blueprint. You can create multiple object instances with one class.
When the speak() method is called, it must know which object called it.

Here the self parameter tells the method which object is called. This is called an instance reference.
zhangsan. speak() is like peo.speak (zhangsan)
'''


Related articles: