python主函数入口_python类 + mian()函数

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)

3

Out[10]:

>>>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)

3

Out[18]:

>>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

57be98347814

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值