Python Pit: Class and Class object type parameter pass and use

Source: Internet
Author: User

Objective

For beginners, Python is really easy to use, after all, dynamic type language, no definition can be used, the type of arbitrary conversion is not too convenient, so Python used to write small scripts, bots and so on, no problem.
However, once used to develop a slightly larger project, such as building a Web application, you will encounter some problems, and generally inexperienced people will fall into some pits. = =...

Keng

First, the pit, the function parameter type is a pit, class and class object this is another hole.
Although it has been understood before with other static type languages (such as C#/java), it is always a bit confusing to change the dynamic type of Python.

Example

Let me use the code to give an example.

First, define two classes, all inheriting from the built-in Exception classes, stating that the two classes are exception classes.

class Error1(Exception):    def__str__(self):        return‘error1‘class Error2(Exception):    def__init__(self):        print(‘error2 init‘)    def__str__(self):        return‘error2‘

Then define the method for handling the exception:

defobject):    print(f‘err:{err.__str__()}‘)defException):    print(err)

Then the test code:

try:    raise Error1exceptas e:    error(e)if1!=2:    error(Error2)

Operation Result:

err:error1  File"/home/test.py"<module>    error(Error2)  File"/home/test.py", line 19, in error    print(f‘err:{err.__str__()}‘)TypeErrormissing‘self‘

The first error() result is nothing wrong, but the second one throws an exception, look at the error message first: TypeError: __str__() missing 1 required positional argument: ‘self‘ , do not provide self parameters, because this parameter is not Error2 an instance of the class, so naturally there are no self parameters.

Here should be a little understand, is called error(Error2) this method, the parameters passed in Error2 is actually Error2 the type itself, not its object, a bit magical, incredibly put a type as a parameter.

How to solve it, very simple, the incoming Error2 object on the line.
The code is as follows:

if1!=2:    error(Error2())

Run results

error2 initerr:error2

There is no problem, the above code there is a error2 way not to use it, to try.

error2(Error2)error2(Error2())

Run results

<class‘__main__.Error2‘>error2 initerror2

It can be seen that when used print(Object) , if it is a type, the returned results are printed only when the type of information is typed, which is the type of the object Object.__str__() .
After understanding the fact is very simple, but Python has no restrictions on the function parameters, even if the method added type hints , but also played a hint, will not do real restrictions or implicit conversion, so sometimes the code to write long dizzy brain swelling, it is easy to fall into the dynamic type pit t_t ...

International practice, put pictures:

About

For more interesting actions please follow my public number: Dealiaxy
Each article is included in my blog: blog.deali.cn

Python Pit: Class and Class object type parameter pass and use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.