Python metadata usage instance and python metadata instance

Source: Internet
Author: User

Python metadata usage instance and python metadata instance

The examples in this article describe the usage of meta-classes in python and share them with you for your reference. The specific method is analyzed as follows:

1. metaclass is a class used to create a class.

2.Type (object): returns the type of an object, which is the same as the value of object. _ class _., Type (name, bases, dict): Creates a new type. name indicates the name of the new class. The value is saved to the _ name _ attribute, and bases indicates the tuple type, the value is saved to _ bases _, and the value of dict is saved to _ dict _.

Copy codeThe Code is as follows: class X:
... A = 1
...
X = type ('x', (object,), dict (a = 1 ))

3. The class is created by default with type (). You can specify the metaclass parameter or inherit from a class when defining the class. This class specifies the metaclass parameter to customize the class creation process.

Copy codeThe Code is as follows: class OrderedClass (type ):
# The return value of this method is the namespace parameter of _ new _. If the namespace value of this method does not exist, it is dict ()
@ Classmethod
Def _ prepare _ (metacls, name, bases, ** kwds ):
Return collections. OrderedDict ()
# Namespace is the class's _ dict __. this dict type object has been filled with corresponding values.
Def _ new _ (cls, name, bases, namespace, ** kwds ):
Result = type. _ new _ (cls, name, bases, dict (namespace ))
Result. members = tuple (namespace)
Return result

Class A (metaclass = OrderedClass ):
Def one (self): pass
Def two (self): pass
Def three (self): pass
Def four (self): pass
Print (A. members)
# ('_ Module _', '_ qualname _', 'one', 'two', 'three ', 'four ')

I hope this article will help you with Python programming.


Python class instantiation

You do not understand the relationship between class variables and instance variables.
In the first example, append is an operation on class variables in init. Therefore, the Instance Object newmen1/2 does not have its own variable a, and class variables are accessed.
If you display a class variable,
Print Men. a, newmen1.a, and newmen2.a are the same. Point to the same variable.
In the second example, init generates the object's own variable a. Note that '= '! When the instance object calls init, each instance object has its own variable a, and you can no longer access the class variable through the instance object. In fact, this writing method is not good.
Then you can display the class variable again,
Print Men. a, newmen1.a, newmen2
> 0 1 2
If you want all objects to share class variables, you can write
Class Men:
A = 0
Def _ init _ (self, B ):
Men. a = B
Def sayHi (self ):
Print 'hello, my name is ', Men.
Instead of using self to represent the public variables of the Instance Object. It will only make you dizzy.

** Prefix usage in python. Here is an example (with points)

Def fun (p, ** args ):
Print args

Fun (1, a = 2, B = 3, c = 4, d = 5)

Related Article

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.