Re: Why doc call `__init__` as a method rather than function?

2023-09-18 Thread scruel tao via Python-list
Thanks for all repliers: @Tony & @Cameron, I do know related stuffs about the dunder methods, and your explanation just make it to be more clear, thank you! @Roel, you just caught everyone here, we do miss it even though we know it and use it regularly! @Clara > its both, depending on how

Why doc call `__init__` as a method rather than function?

2023-09-15 Thread scruel tao via Python-list
```python >>> class A: ... def __init__(self): ... pass ... >>> A.__init__ >>> a = A() >>> a.__init__ > ``` On many books and even the official documents, it seems that many authors prefer to call `__init__` as a "method" rather than a "function". The book PYTHON CRASH COURSE mentioned

We can call methods of parenet class without initliaze it?

2023-03-15 Thread scruel tao
The following code won’t be allowed in Java, but in python, it works fine: ```python class A: A = 3 def __init__(self): print(self.A) def p(self): print(self.A) self.A += 1 class B(A): def __init__(self): print(2) self.p()

Re: How to exit program with custom code and custom message?

2023-03-13 Thread scruel tao
Lars: > I totally understand your reasoning here, but in some way it follows the unix > philosophy: Do only one thing, but do that good. I understand, python is not strongly typed, so `sys.exit` will be able to accept any types parameters rather than just integer. In order to handle such

转发: How to exit program with custom code and custom message?

2023-03-13 Thread scruel tao
Chris: > It doesn't actually take a list of arguments; the square brackets indicate that arg is optional here. Oh, I see, it seems that I mistunderstood the document. > but for anything more complicated, just print and then exit. > It's worth noting, by the way, that sys.exit("error message")

How to exit program with custom code and custom message?

2023-03-13 Thread scruel tao
Currently, I use `sys.exit([arg])` for exiting program and it works fine. As described in the document: > If another type of object is passed, None is equivalent to passing zero, and > any other object is printed to stderr and results in an exit code of 1. However, if I want to exit the program

Re: Add angle brackets for required args in argparse

2023-02-22 Thread scruel tao
Thank you for your workarounds, Mark Bourne. `metavar` argument should be sufficient for infrequent use scenarios, and I will consider to use the custom help formatter if necessary. >>> That's a bit closer to what you asked for, since the required argument >>> shown in the error message doesn't

Add angle brackets for required args in argparse

2023-02-15 Thread scruel tao
If we have the following code: ``` parser = argparse.ArgumentParser(description="test") parser.add_argument('path') ``` Run it without args, will get error message: ``` usage: test.py [-h] path test.py: error: the following arguments are required: path ``` However, I hope the message can be as

Change the identation base value?

2023-02-15 Thread scruel tao
Currently, we have following PEP: PEP 8: E114 indentation is not a multiple of 4 (comment) However, I wonder how many people are using 2 spaces as their code indentation in projects? Should it be nesserary for us to change this PEP from “multiple of 4” to “multiple of 2”? --