当前位置:网站首页>Ain 0722 sign in

Ain 0722 sign in

2022-07-24 05:26:00 Xinxin vegetation

0722 Sign in

One . Underline in Python Function and significance of

1 Underline _

If the result of the last statement in the session is not assigned to a variable through the assignment statement , Then the value is assigned to “_”
And the calculator ans It's kind of similar ( If there is no assignment )
 Insert picture description here

2 As a single underscore before variables “_”, Such as _name

The module where the variable is located / The variable can also be accessed outside the class , But better not (protected)

class Student:
    def __init__(self, name, age):
        self._name = name
        self.age=age
stu = Student('hello',30)
print(stu.age)    #30
print(stu._name)  #hello

It is worth noting that
With "_“”__" The beginning of the name will not from<>import∗ Import in this form

Define modules test1

''' modular test1.py'''

num = 20
_num = 40
__num = 60
__num__ = 80

from< test1 >import ∗

''' modular test2.py'''
from test1 import *

print(num) #20
print(_num) # Report errors ,name '_num' is not defined
print(__num) # Report errors ,name '__num' is not defined
print(__num__) # Report errors ,name '__num__' is not defined

import test1

''' modular test2.py'''
import test1

print(test1.num) # Output 20
print(test1._num) # Output 40
print(test1.__num) # Output 60
print(test1.__num__) # Output 80


3 As double underscores before variables “__”, Such as __name

The module where the variable is located / The variable cannot be accessed outside the class , This is a private variable (private)
reason :
Python The interpreter puts out __xxx Variable changed to _classname__xxx
therefore , Can still pass _classname__xxx To visit __xxx Variable

class Student:
    def __init__(self, name, age):
        self.__name = name
        self.age=age

stu = Student('Vicent',30)
print(stu.age) # Output 30
print(stu._Student__name) # Output hello
print(stu.__name) # Report errors :AttributeError: 'Student' object has no attribute '__name'

4 Double underscores before and after variables Such as __ init __ ,__ name __

Python Built in special variables , You can visit anywhere
If users do not customize variables and functions in this naming form, they will not conflict with the methods defined by the system
Generally, it is only by rewriting these methods that we can use
for example , When defining a class , I often override “init” Method .

class Student:
    def __init__(self, name, age):
        self.__name = name
        self.age=age

except __ init __
also __ del __ 、__ add __ 、__ getitem __ etc.
And the overall situation __ file __ 、__ name __ etc. .

Two . conda Basic instructions and operations

All of the following The name of the environment is newd2l For example , Readers should send newd2l Replace with the desired environment name
All of the following The name of the package is d2l For example , Readers should send d2l Replace with the package name you need

 Create an environment 
conda create -n newd2l python=3.7

 Look at all the environments  
conda env list

 Switching environment 
activate newd2l

 Delete environment 
conda remove -n newd2l --all

 Check whether the package is normal 
pip check d2l

 Download a package 
pip install d2l

 Unload a package 
pip uninstall d2l
 perhaps 
conda remove --name newd2l  d2l

 Tips  :  Better first uninstall Again install Update a package 

 Check the version number of the package 
conda list d2l



原网站

版权声明
本文为[Xinxin vegetation]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/205/202207240515145521.html