Python--Class类定义

Python–Class类定义

一、Class类
1、__dict__的特性可自动实例化对象,免了在__init__方法中类似于self.something = something的方法

class Person:
    def __init__(self,_obj):
        self.__dict__.update(_obj)

class Person:
    def __init__(self,_obj):
        self.name = _obj['name']
        self.age = _obj['age']
        self.energy = _obj['energy']
        self.gender = _obj['gender']
        self.email = _obj['email']
        self.phone = _obj['phone']
        self.country = _obj['country']

2、__call__(self)
在Python中,函数其实是一个对象:

>>> f = abs
>>> f.__name__
'abs'
>>> f(-123)
123    #由于 f 可以被调用,所以,f 被称为可调用对象

一个类实例也可以变成一个可调用对象,只需要实现一个特殊方法__call__(self)

class Person(object):
    def __init__(self, name, gender):
        self.name = name
        self.gender = gender

    def __call__(self, friend):
        print 'My name is %s...' % self.name
        print 'My friend is %s...' % friend

对 Person 实例直接调用:

>>> p = Person('Bob', 'male')
>>> p('Tim')
My name is Bob...
My friend is Tim...

二、python常见函数用法
1、 product用于求多个可迭代对象的笛卡尔积

product(A, B) 等价于 ((x,y) for x in A for y in B)
product(A,repeat=3) 等价于 product(A,A,A)

#product(‘ABCD’, ‘xy’) --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111

from itertools import product
for i, j in product(range(f), repeat=2):

在这里插入图片描述
2、python输出:
(1)多个%s使用方法

a = "wry"
b = "zjl"
c = "xxx"
print "a=%s, b=%s, c = %s" %(a,b,c)
>>>a=wry, b=zjl, c = xxx  #结果

(2).format()函数使用

image.save('results_{}/s{}-c{}-l{}-e{}-sl{:4f}-cl{:4f}.jpg'
               .format(num, para['style_weight'], para['content_weight'], para['lr'], para['epoch'],
                       para['style_loss'], para['content_loss']))
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值