directory

Object properties and class properties

Class attributes and methods

Class private properties

Methods of a class

Class private methods

Dynamically add methods and properties to the instance and use them

@property

Operator overloading

Mail sending program


Object properties and class properties

In Python, the characteristics of an object are also called attributes. The behavior that it has is also called a method

Conclusion: Object = property + method

In Python, objects with the same properties and methods are grouped into a class.

Class attributes and methods

Class private properties

__private_attrs: Begins with two underscores, declaring that the property is private and cannot be used or accessed directly outside the class. Self.__private_attrs when used in methods inside a class.

Methods of a class

Inside a class, the def keyword is used to define a method. Unlike normal function definitions, the class method must contain the first argument, self, which represents an instance of the class.

The name self is not binding. You can also use this, but it’s best to stick to the convention and use self.

Class private methods

__private_method: starting with two underscores, declares that this method is private and can only be called inside the class, not outside it. Self. __private_methods.

class Person(object) :
    The attributes here are class attributes and are called by the class name
    name = "aaa"
    # Here are the object properties
    def __init__(self,name) :
        self.name = name


a = Person("kkk")
print(Person.name)
print(a.name)
Copy the code

If there are no object attributes, object attributes take precedence over class attributes

class Person(object) :
    The attributes here are class attributes and are called by the class name
    name = "aaa"
    # Here are the object properties
    def __init__(self,name) :
        pass
        #self.name = name


a = Person("kkk")
print(Person.name)
print(a.name)
Copy the code

Dynamically adding attributes to an object only applies to the current object, not to other objects created by the class

a = Person("kkk")
Add attributes to objects dynamically
a.age = 18
print(a.age)
Copy the code

Deleting the name attribute in an object invokes the name attribute in the classCopy the code
class Person(object) :
    The attributes here are class attributes and are called by the class name
    name = "aaa"
    # Here are the object properties
    def __init__(self,name) :
        self.name = name

a = Person("kkk")
print(a.name)
# delete the name attribute from the object, call the name attribute from the class
del a.name
print(a.name)
Copy the code

 

Dynamically add methods and properties to the instance and use them

from types import MethodType
Create an empty class
class Fun() :
    pass

a = Fun()
# Add attributes dynamically, which is characteristic of dynamic languages (flexibility)
a.name = "Jerry"

# dynamic add method
def say(self) :
    print("my name is " + self.name)
a.speak = MethodType(say,a)
a.speak()
Copy the code

If you want to limit the number of instance attributes you can add, only name,age,height, and weight attributes are allowed

Solution: When defining a class, define a special attribute (__slots__) that limits dynamically added attributes

Example:

class Fun() :
    __slots__ = ("name"."age"."height"."weight")

a = Fun()
# Add attributes dynamically, which is characteristic of dynamic languages (flexibility)
a.name = "Jerry"
print(a.name)
a.age = 18
print(a.age)
a.sex = "man"
print(a.sex)
Copy the code

 

@property

Previously, the __init__() attribute was exposed directly, which was unsafe

class Fun(object) :
    def __init__(self,name) :
        Attributes are exposed directly
        self.name = name

a = Fun("lisi")
Attributes are exposed directly
# No security, no filtering of data
a.name = "zhangsan"
print(a.name)
Copy the code

Use methods that restrict access

class Fun(object) :
    def __init__(self) :
        pass
        #self.name = name
        # restrict access
    def getName(self) :
        return self.__name
    def setName(self,name) :
        if len(name)>10:
            self.__name = ""
        else:
            self.__name = name

a = Fun()
# Use restricted access
a.setName("zhangsan")
print(a.getName())
Copy the code

Using property: Allows you to use point syntax for properties that have restricted access

class Fun(object) :
    def __init__(self) :
        pass
        #self.name = name
        # restrict access
    # If two methods have the same name, the following method overrides the previous one
    @property
    def Name(self) :
        return self.__name
    @Name.setter  Get rid of the underscore. Setter
    def Name(self,name) :
        if len(name)>10:
            self.__name = ""
        else:
            self.__name = name

a = Fun()
a.Name = "zhangsan" # equivalent to calling setName()
print(a.Name)   # equivalent to calling getName()
Copy the code

 

Operator overloading

Different types can be interpreted differently by addition

Operator + : __add__

class Sum(object) :
    def __init__(self,num) :
        self.num = num

a = Sum(2)
b = Sum(3)
print(a + b)
Copy the code

class Sum(object) :
    def __init__(self,num) :
        self.num = num
    Operator overloading
    def __add__(self,other) :
        return Sum(self.num + other.num)
    def __str__(self) :
        return "sum = "+str(self.num)

a = Sum(2)
b = Sum(3)
print(a + b)
Copy the code

A + b is equivalent to A. __add__(b)

print(a + b)
print(a.__add__(b))
Copy the code

 

Mail sending program

# Emailing library
import smtplib
# Email text
from email.mime.text import MIMEText

# SMTP server
SMTPServer = "smtp.163.com"

# Email address
Sender = "[email protected]"  # Own email address
Sender email password
passwd = "1234567890a" # Authorization password

Set the content to be sent
message = "this is a email to you"
# Convert to email text
msg = MIMEText(message)

# Email subject
msg["Subject"] = "boynextdoor"
# the sender
msg["From"] = Sender

Create an SMTP server
mainServer = smtplib.SMTP(SMTPServer,25)
# Login email
mainServer.login(Sender,passwd)
# Send email
mainServer.sendmail(Sender,["Email address of the person you're sending it to."],msg.as_string())
# exit email
mainServer.quit()
Copy the code

 

 

 

Learn together and make progress together. If there are any mistakes, please comment