简体   繁体   中英

passing multiple *arg(or **kwargs) to an object by explicitly calling their name

This question came up to me when I was learning *arg and **kwargs in Python. Taking this example:

class Person1():
    def __init__(self, name, age, *petdogs):
        self.name = name
        self.age = age
        self.petdogs = list(petdogs)

    def show_dogs(self):
        for petdog in self.petdogs:
            print(petdog)
Dummy = Person1("Dummy", 16, "A", "B", "C")
Dummy.show_dogs()
#Output:
#A
#B
#C

Is it possible to pass multiple * and ** arguments into an object?

Here is what I tried:

class Person2():
    def __init__(self, name, age, *petdogs, *petcats):
        self.name = name
        self.age = age
        self.petdogs = list(petdogs)
        self.petcats = list(petcats)
    def show_dogs(self):
        for petdog in self.petdogs:
            print(petdog)
    def show_cats(self):
        for petcat in self.petcats:
            print(petcats)

then

Dummy2 = Person2(name = "Dummy", age = 16, petdogs = ("A", "B", "C"), petcats = ("D", "E"))
print("pet dogs:")
Dummy2.show_dogs()
print("pet cats:")
Dummy2.show_cats()

I want to see the out puts to be like the example above

pet dogs:
A
B
C
pet cats:
D
E

Of course it is not working, but I am wondering if there is a way to achieve by using *?

The *args construct acts in a function as a placeholder for the rest of the arguments passed, so you can't have two 'placeholder for the rest of the arguments', because technically there's only one 'rest of arguments'. Example:

def f(a, b, *args):
    print(f'a = {a}, b = {b}, rest = {args}')

f(1, 2, 3, 4, 5, 6) # => a = 1, b = 2, rest = (3, 4, 5, 6)

**kwargs is similar in the spirit, but applies to arguments passed in the keyword=value fashion: the rest of any keyword-arguments passed are going to be displayed inside the function in the form of a dict of the name kwargs . Example:

def f(a, b, c, *args, **kwargs):
    print(f'a = {a}, b = {b}, rest = {args}, rest-kw = {kwargs}')

f(1, 2, 3, 4, 5, 6, d=7, e=8) #=> a = 1, b = 2, rest = (3, 4, 5, 6), rest-kw = {'d': 7, 'e': 8}

With what you want to do, there technically no 'rest' you want to express, so you can go with:

class Person2():
    def __init__(self, name, age, petdogs, petcats):
        self.name = name
        self.age = age
        self.petdogs = list(petdogs)
        self.petcats = list(petcats)
    def show_dogs(self):
        for petdog in self.petdogs:
            print(petdog)
    def show_cats(self):
        for petcat in self.petcats:
            print(petcats)

and

Dummy2 = Person2(name = "Dummy", age = 16, petdogs = ("A", "B", "C"), petcats = ("D", "E"))
print("pet dogs:")
Dummy2.show_dogs()
print("pet cats:")
Dummy2.show_cats()

will work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM