In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the knowledge points that are most easily ignored in Python object-oriented programming". In daily operation, I believe many people have doubts about the knowledge points that are most easily ignored in Python object-oriented programming. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods, hoping to help you answer the doubts about "what knowledge points are most easily ignored in Python object-oriented programming?" Next, please follow the editor to study!
1. _ _ init__ (self,...)' Constructor 'method
_ _ init__ (self,...) Method is not really a constructor, it does not create a new object, Python creates the object through the function operator (). After the interpreter creates an instance, _ _ init__ (self,...) is called first. Method to define additional behavior, if not defined or overridden _ _ init__ (self,...) Method, no special action is applied to the instance, and its object is returned directly, and the instantiation process is complete. The return value of the function should be None, and if any object is returned, it will cause a TypeError exception.
Class ReturnInt: def _ _ init__ (self): return 1ri=ReturnInt () Traceback (most recent call last): File "C:/Users/PycharmProjects/untitled/Study EXP/Q.py", line 5, in ri=ReturnInt () TypeError: _ _ init__ () should return None
Override _ _ init__ (self,...) of a subclass Method does not automatically call _ _ init__ (self,...) of the base class. So if you need to call the _ _ init__ (self,...) of the base class in the subclass, you need to make it clear.
Class C (P): def _ _ init__ (self): P.The inituation _ (self)
We can use the super () function to rewrite the above code more easily and efficiently:
Class C (P): def _ _ init__ (self): super (C _ init__). _ _ init__ ()
Instead of providing an explicit parent class, the super () function will help us find the corresponding parent class and then make it easy to call the relevant properties.
2. _ _ new__ (cls,...)' Constructor 'method
Call _ _ new__ (cls,...) The instance may not have been created, as opposed to _ _ init__ (self,...) Method needs to pass a different reference to the instance, _ _ new__ (cls,...) Pass in a reference to the current class. Can be used to derive built-in types and instantiate immutable objects.
3. Class attribute
Class properties are only bound to the classes they define, including data properties (static variables) and methods. The method is defined in the class, but can only be called by the instance. If there is no instance property with the same name as the data property of the class, the data property of the class can also be accessed through the instance, but cannot be modified.
Special class attributes:
The name of class C (string), the name of class C, the name of class C (string), the name of class C, the name of class C (string), the name of class C, the name of class C, the name of class C (string), the name of class C, the name of class C (string), the name of class C, the name of class C (string), the name of class C
4. Instance property
The instance has only data properties, that is, class properties. An instance property can be created dynamically, but if the property is created in a conditional statement and the conditional statement is not executed, the property does not actually exist, and an error will occur if the property is accessed by later code.
_ _ init__ (self,...) with default parameters An instance can be initialized more efficiently, eliminating the hassle of explicitly passing values, but the default parameters should be immutable objects and should always be vigilant when using variable objects such as lists and dictionaries.
Class demo_list: def _ init__ (self, l = []): self.l = l def add (self, ele): self.l.append (ele) def appender (ele): obj = demo_list () obj.add (ele) print obj.lif _ name__ = "_ _ main__": for i in range (5): appender (I) [0] [0,1] [0,1,2,3] [0,1,2,3] 4]
Because the default parameter is only calculated once and will not be reused, in the above example, although a different new instance is used, the constructor's parameter uses a reference to the list as the default parameter. so each time the instance property points to the space where the list is located.
Use the built-in function dir () to display class and instance properties, which have a special property of _ _ dict__, which consists of dictionaries and contains all the properties of an instance.
'# means that when ReturnInt is a new class The result corresponding to dir 'class ReturnInt: # class ReturnInt (object): def _ _ init__ (self): passri=ReturnInt () ri.name='a'ri.age=10print ri.__dict__print dir (ri) {' age': 10, 'name': 'a'} [' _ doc__','_ init__','_ module__', 'age',' name'] # ['_ class__','_ delattr__' '_ _ dict__',' _ _ doc__','_ _ format__','_ _ getattribute__','_ _ hash__','_ _ init__','_ _ module__','_ _ new__','_ _ reduce__','_ _ reduce_ex__','_ repr__','_ setattr__','_ sizeof__','_ _ str__' '_ _ subclasshook__',' _ _ weakref__', 'age',' name']
You can see from the above program that in addition to the instance properties we added, the instance has many other properties, which are the built-in type properties of the instance.
5. Bindings and method calls
The method of one of the class attributes can only be called when the class to which it belongs has an instance, when there is an instance, the method is considered to be bound to that instance, and when there is no instance, the method is unbound.
When a class has an instance, such as class MyClass, you can automatically pass mc, that is, self, into foo () through a call to mc.foo (), without requiring us to explicitly write it as mc.foo (self). At that time, the self parameter must be passed when there is no instance and an unbound method needs to be called.
At this point, the study on "what are the most easily ignored knowledge points in Python object-oriented programming" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.