python init构造函数___Python中的init\uuuu构造函数

您可以使用一些默认参数,并根据填充的参数来决定使用哪个参数:class Block(object):

def __init__(self, height, width, coord_tuple=None, coord_x=None, coord_y=None):

if coord_tuple:

self.x = coord_tuple[0]

self.y = coord_tuple[1]

else:

self.x = coord_x

self.y = coord_y

self.height = height

self.width = width

return

def print_coord(self):

print("Height: {}\nWidth: {}\nX: {}\nY: {}"

.format(self.height, self.width, self.x, self.y))

return

# tuple instantiations

b1 = Block(1, 2, (1,2))

b2 = Block(3, 4, coord_tuple=(3,4))

# x/y instantiation

b3 = Block(5, 6, coord_x=5,coord_y=6)

b1.print_coord()

b2.print_coord()

b3.print_coord()

产生:Height: 1

Width: 2

X: 1

Y: 2

Height: 3

Width: 4

X: 3

Y: 4

Height: 5

Width: 6

X: 5

Y: 6

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值