简体   繁体   中英

What is the difference between stating and not stating init function parameters?

What is the difference between the following?

My code gave an incorrect result when I used the first piece. https://leetcode.com/submissions/detail/752510950/

It worked when replaced with a second piece. https://leetcode.com/submissions/detail/752513133/

class MyStack:

    def __init__(self,initial=[]):
        self.initial = initial
#VS

class MyStack:
    def __init__(self):
        self.initial = []

In the first case, you can make an object and pass it a list if you want

my_stack = MyStack(['a', 'b', ... 'z'])

and if you don't pass anything it will automatically initialize an empty list.

But in the second example, you can't pass any arguments when you create the object. Immediately after creation, you have an object with an empty list.

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