美文网首页
python类 + mian()函数

python类 + mian()函数

作者: 啊哈JC熙 | 来源:发表于2018-03-28 16:48 被阅读124次

main函数

import sklearn
import numpy as np


print(sklearn)

# print("hello")

ra = np.random.randn(5)
# print(ra)


def ss():
    a = 1
    b = 2
    print(a+b)
    pass


# ss()


def plot():
    from mpl_toolkits.mplot3d import axes3d
    import matplotlib.pyplot as plt
    from matplotlib import cm

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    X, Y, Z = axes3d.get_test_data(0.05)
    cset = ax.contour(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)
    cset = ax.contour(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)
    cset = ax.contour(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)

    ax.set_xlabel('X')
    ax.set_xlim(-40, 40)
    ax.set_ylabel('Y')
    ax.set_ylim(-40, 40)
    ax.set_zlabel('Z')
    ax.set_zlim(-100, 100)

    plt.show()


if __name__ == '__main__':
    # ss()
    plot()
    pass

if name == 'main': python主函数的入口,而在其中过程画的东西(函数,print语句等等...)也会被执行。

python 重新载入模块

>>>import importlib
>>>importlib.reload(foo)
<module 'sklearn' from '/anaconda3/lib/python3.6/site-packages/sklearn/__init__.py'>
3
Out[10]: <module 'foo' from '/Users/lijuncheng/PycharmProjects/Conda1/foo.py'>
>>>foo.new()
This is new reload(hello)

类的编写

代码

class ClassCreat:
    # 用这个方法控制如何初始化对象
    def __init__(self, c_int, c_string,  c_list=[]):
        # 把含有默认值的参数放在了不含默认值的参数的前面
        self.cint = c_int
        self.clist = c_list
        self.cString = c_string
        pass

    def read(self):
        print(self.cint)
        print(self.clist)
        print(self.cString)
        pass
    pass

调用

>>importlib.reload(foo)
<module 'sklearn' from '/anaconda3/lib/python3.6/site-packages/sklearn/__init__.py'>
3
Out[18]: <module 'foo' from '/Users/lijuncheng/PycharmProjects/Conda1/foo.py'>
>>cc = foo.ClassCreat(1,'string',[1,2,3,4,5])
>>cc.read()
1
[1, 2, 3, 4, 5]
string
>>cc = foo.ClassCreat(1,'string')
>>cc.read()
1
[]
string

错误

1.non-default argument follows default argument
把含有默认值的参数放在了不含默认值的参数的前面

2.TypeError: object() takes no parameters
对象没有初始化成功
init写成了int

屏幕快照 2018-03-28 下午4.49.17.png

相关文章

网友评论

      本文标题:python类 + mian()函数

      本文链接:https://www.haomeiwen.com/subject/slfucftx.html