Emily Variable in Python

Asked 2 years ago, Updated 2 years ago, 19 views

Hi, how are you? I'm studying on Python, and I'm asking you a question because the following necessary situation came up. When defining a function, I want to define a variable that can be omitted.

class MyQueue(object):
        def __init__(self, Tree = None)
            self.list = [Tree]

I think. It can be omitted, but it still allows me to put it in when I want to. by the way None and Emily are different things. None wasn't pointing at anything, but it was still taking over. Can't we make it possible to omit the variable Tree instead of None?

class MyQueue(object):
        def __init__(self, Tree = None):
            if Tree != None:
                self.list = [Tree]
            else:
                self.list = []

Besides this, does Python itself support keywords such as Emily? I don't know what to search for keywords, so I ask questions like this.

python

2022-09-22 20:23

2 Answers

self.list = [Tree]

If you write the program as shown above, it will enter a two-dimensional array.

[ [None] ] 

I think you're trying to write a queue, so I think you need a one-dimensional array.

I think you can modify the above phrase as below.

class MyQueue(object):

    def __init__(self, Tree=None):
        self.list = Tree

If it is None when used as above, self.There may be errors in the process of handling the list, so please be careful.

Modify

I guess I answered the wrong answer because I guessed wrong and didn't get the gist of the question. I'm sorry.

If you want to modify a little bit from the code you wrote

            if Tree:
                self.list = [Tree]
            else:
                self.list = []

If you do this, I think you can use the same moves more neatly.


2022-09-22 20:23

The question is ambiguous.

You can make a version without arguments by overloading init.

class MyQueue(object):
        def __init__(self, Tree = None)
            self.list = [Tree]

        def __init__(self):
            pass


2022-09-22 20:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.