2021-11-12PY8

本文通过一个Python类的实例展示了如何定义类变量、对象变量,并实现对象相加及临时变量的设置。类`Person`包含了类变量class_type和对象变量name、age,并定义了`print_info`方法用于打印这些变量。此外,还实现了`__str__`方法,使得打印对象时输出特定字符串。通过创建`zhangsan`和`lisi`两个对象,实现了对象相加返回2,同时为对象添加临时变量`temp_var`展示了动态修改类变量的能力。

“”"
1.定义一个类(不限定定义哪个类):
要求: a.需要有一个类变量
b.需要有>=2个的对象变量
c.定义一个方法:打印类变量和对象变量
d.使用print打印对象->输出为This is a object
e.实例化两个对象:且两个对象相加等于2
f.为对象添加一个临时变量temp_var
“”"
class Person:
class_type = “Person Class”
def init(self, name, age):
self.name = name
self.age = age
def print_info(self):
print(“类变量:”, Person.class_type)
print(“对象变量:”, self.name, self.age)

def __str__(self):
    return "This is a object"

def __add__(self, other):
    return 2

zhangsan = Person(“zhangsan”, 18)
print(zhangsan.print_info())
lisi = Person(“lisi”, 20)
print(zhangsan + lisi)

产生了一个对象的临时变量 class_type

zhangsan.class_type = “temp variable”
print(zhangsan.class_type)
print(lisi.class_type)
在这里插入图片描述

conda remove -y numpy D:\anaconda3\Lib\site-packages\conda\base\context.py:201: FutureWarning: Adding 'defaults' to channel list implicitly is deprecated and will be removed in 25.3. To remove this warning, please choose a default channel explicitly with conda's regular configuration system, e.g. by adding 'defaults' to the list of channels: conda config --add channels defaults For more information see https://docs.conda.io/projects/conda/en/stable/user-guide/configuration/use-condarc.html deprecated.topic( Channels: - defaults Platform: win-64 Collecting package metadata (repodata.json): done Solving environment: done ## Package Plan ## environment location: D:\anaconda3\envs\pytorch removed specs: - numpy The following packages will be REMOVED: blas-1.0-mkl bottleneck-1.4.2-py39hc99e966_0 contourpy-1.2.1-py39h214f63a_1 cycler-0.11.0-pyhd3eb1b0_0 fonttools-4.60.1-py39h02ab6af_0 freeglut-3.4.0-h8a1e904_1 freetype-2.13.3-h0620614_0 h5py-3.14.0-py39h6fc9867_0 hdf5-1.14.5-ha36df97_2 icc_rt-2022.1.0-h6049295_2 icu-58.2-ha925a31_3 imbalanced-learn-0.12.3-py39haa95532_1 importlib_resources-6.5.2-py39haa95532_0 intel-openmp-2023.1.0-h59b6b97_46320 joblib-1.5.2-py39haa95532_0 jpeg-9f-ha349fce_0 keras-preprocessing-1.1.2-pyhd3eb1b0_0 kiwisolver-1.4.4-py39hd77b12b_0 krb5-1.20.1-h5b6d351_0 lcms2-2.16-h62be587_1 lerc-4.0.0-h5da7b33_0 libclang-14.0.6-default_hb5a9fac_2 libclang13-14.0.6-default_h8e68704_2 libdeflate-1.22-h5bf469e_0 libpng-1.6.39-h8cc25b3_0 libpq-12.20-h70ee33d_0 libtiff-4.7.0-h404307b_0 libwebp-base-1.3.2-h3d04722_1 m2w64-gcc-libgfortran-5.3.0-6 m2w64-gcc-libs-5.3.0-7 m2w64-gcc-libs-core-5.3.0-7 m2w64-gmp-6.1.0-2 m2w64-libwinpthread-git-5.0.0.4634.697f757-2 matplotlib-3.9.2-py39haa95532_1 matplotlib-base-3.9.2-py39he19b0ae_1 mkl-2023.1.0-h6b88ed4_46358 mkl-service-2.4.0-py39h827c3e9_2 mkl_fft-1.3.11-py39h827c3e9_0 mkl_random-1.2.8-py39hc64d2fc_0 mpi-1.0-msmpi mpi4py-4.0.3-py39h827c3e9_0 msmpi-10.1.1-had4844c_0 msys2-conda-epoch-20160418-1 numexpr-2.10.1-py39h4cd664f_0 numpy-1.26.4-py39h055cbcc_0 numpy-base-1.26.4-py39h65a83cf_0 openjpeg-2.5.2-h9b5d1b5_1 opt_einsum-3.3.0-pyhd3eb1b0_1 pandas-2.3.3-py39ha5e6156_0 pillow-11.1.0-py39hea0d53e_1 ply-3.11-py39haa95532_0 pybind11-abi-5-hd3eb1b0_0 pyparsing-3.2.0-py39haa95532_0 pyqt-5.15.10-py39h5da7b33_1 pyqt5-sip-12.13.0-py39h827c3e9_1 python-tzdata-2025.2-pyhd3eb1b0_0 pytz-2025.2-py39haa95532_0 qt-main-5.15.2-h879a1e9_9 scikit-learn-1.6.1-py39h585ebfc_0 scipy-1.13.1-py39h8640f81_1 seaborn-0.13.2-py39haa95532_3 sip-6.7.12-py39h5da7b33_1 tbb-2021.8.0-h59b6b97_0 threadpoolctl-3.5.0-py39h9909e9c_0 Downloading and Extracting Packages: Preparing transaction: done Verifying transaction: done Executing transaction: done这是卸载完成了吗
最新发布
10-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值