This article is participating in Python Theme Month. See the link for details

The most important factors in a happy life are something to do, someone to love and something to hope for in life.

Previously: Zhuang interview, the result of the interviewer anxious to have dinner ~ again appointment this time to meet, I don’t know what questions will be asked?

Interviewer: The young man arrived on time. Let’s continue our conversation today. Have you got a date?

Me :(thinking that the interviewer is so enthusiastic that if I don’t have a partner, why would he introduce me to one?) Thanks to you, it’s already here.

Interviewer: Since you are dating someone, let’s talk about dating someone today.

I :(clear in heart, originally is want text face object, no problem of no problem) good, you say please.

Interviewer: Python is an object-oriented language. How do you understand the concept of objects?

I: Object-oriented in python have always had an abstraction, is everything all objects, in terms of my personal understanding, object is some data and some operation data of a set of methods, such as our custom a class, so there will be some data and the method in this class, like the toolkit, it has both the screw and screw driver, Together make up the toolbox object, the screw is the data and the screwdriver is the method.

Interviewer: You talk about object binding methods, static methods, and class methods?

I: Object binding method is the class instantiation of the object can call the method directly, will be the strength as the first parameter to pass to the method, if the class call this method need to pass a parameter, is the example of this class; The classmethod is classmethod, which is a decorator that decorates a method defined in the class as a classmethod. When a classmethod is called, the class is passed to the method as the first argument. A staticmethod is a decorator. It does not automatically pass parameters, either to the class invocation or to the object invocation instantiated by the class. Consider using static methods when you do not need any information or attributes from the class or examples.

Interviewer: Tell me more about reflexes.

Me: Reflection in Python refers to manipulating an object’s properties through a string, and involves the use of four built-in functions

getattr(obj, 'name') gets the property value of the objecthasattr(obj, 'name') determines whether the object has an attribute valuesetattr(obj, 'name') modifies/increases the property value of an objectdelattr(obj, 'name') deletes the properties of the objectCopy the code

By using reflection, you can analyze the properties of an object in a program without knowing what properties it has in advance. The underlying principle of reflection is to view an object property through the dir method, and then reflect the value of the property through the __dict__ dictionary through the string to the real property.

Reflection is a very flexible way to manipulate an object’s properties, such as django’s CBV. Using reflection, the get or POST methods written in a view class are automatically called depending on the request.

Interviewer: Have you ever written metaclasses, or know anything about metaclasses

Me :(to be honest) I haven’t written metaclasses in my company, but I know a little bit about how metaclasses work.

Interviewer: Yeah, that’s okay. Tell me what you know about metaclasses.

Me: A class instantiation produces an object, and in Python a class is an object, and a metaclass instantiation yields a class object. All classes defined by the class keyword and the built-in classes are generated by instantiation of the metaclass Type. We can customize the metaclass by controlling the steps of calling the class. When calling the class, we first execute the __new__ method in the class, which must return an empty object, that is, create an instance, If there is no return value, the class’s __init__ method is not run when the object is instantiated. The __init__ method is the initialization instance that sets the initial value for the empty object. If you want an object to be called in parentheses, you add a __call__ method to the object’s class. The __call__ method in the metaclass will first call the __new__ method in the metaclass, then call the __init__ method in the metaclass. Finally, the __call__ method in the metaclass will return an initialized object.

Interviewer: Good grasp of principle, metaclass is probably such a thing, ok young man, you find this object is good, object oriented to this first, next time we will talk about the network and concurrency this.

I: ok ~ you go well