两种python类传参方式

'''
@Author: Fan LR
@email: [email protected]
@csdn: https://i.csdn.net/#/uc/profile
@Date: 2019-12-18 14:17:09
@Description: 两种python类传参方式
'''



class A(object):
    def __init__(self,people1,people2):

        self.people1=people1
        self.people2=people2

    def B(self):
        print(self.people1,self.people2)


    def C(self):
        print(self.people1)


a=A('张三','李四')
a.B()
a.C()


class AA(object):
    # def __init__(self,people1,people2):

    #     self.people1=people1
    #     self.people2=people2

    def B(self,people1,people2):
        print(people1,people2)


    def C(self,people1):
        print(people1)

aa=AA()
aa.B('asd','asdss')
aa.C('dfdff')

#元组和,字典传参#高级

class f():

    def __init__(self, *args, **kwargs):
        print('args Is', args)
       # args Is ('5', 'fff', 3, ' ')  
        print('kwargs Is', kwargs)
       # kwargs Is {'kwargs': {'a': 1}}
        print(type(kwargs))
        kwargs = kwargs.get('kwargs')
        # kwargs = kwargs['kwargs']
        self.a = kwargs.get('a')
        print('a is ', self.a)
        self.n = args[0]
        self.out = 1
        return super().__init__()

    def xx(self):
        for i in range(1, int(self.n)+1):
            self.out = self.out * i
        print(self.out)

# *args 为tuple类型入参,**kwagrs 为dict类型入参
x = f('5', 'fff', 3, ' ', kwargs={'a': 1}) 22 
x.xx()

你可能感兴趣的:(两种python类传参方式)