python original class class creation process and method details

  • 2021-07-24 11:05:19
  • OfStack

Today, I will introduce you to the knowledge related to class in python...

Gets the class name of the object

python is an object-oriented language. For python, it is necessary for us to study and understand some knowledge deeply

First of all, we all know that to get the class corresponding to an object, we need to use class for retrieval.

But if we are just a simple assignment statement, can we use it like this? Let's look at the following code:


num=10
string='abc'
class MainClass:
  pass
p=MainClass()
 
print(num.__class__)
# output: <class 'int'>
print(string.__class__)
# output: <class 'str'>
print(p.__class__)
# output: <class '__main__.MainClass'>

From the above code, we can see that whether we are a simple assignment operation or we define a class ourselves and generate an object from this class. We can all use __class__ to query the method corresponding to the object. This should be understood by most people. Keep going down

Take the class in our eyes as an object

Just now, we got three class names, str, int and our custom MainClass, by __class.

But if we continue to regard them as objects, who are the corresponding classes? Let's print out


print(int.__class__) #  Or write this: print(num.__class__.__class__)
# output: <class 'type'>
print(str.__class__)
# output: <class 'type'>
print(MainClass.__class__)
# output: <class 'type'>

class 'type' What the hell is this?

It is called the primitive class, which is the class that creates other classes …

Someone wants to ask, is type the ancestor? Are there any ancestors? You can verify it yourself, according to the above method


print(type.__class__)
output: <class 'type'>

This time I gave up. type has no ancestors on it. Haha...

Why type

type is a function that we often use, for example, an object. If we want to know what type it is, we will use type (xxx)

So what exactly is type? Look at the source code …


class type(object):
  """
  type(object_or_name, bases, dict)
  type(object) -> the object's type
  type(name, bases, dict) -> a new type
  """
  ... ...
  ... ...

When we see the comments, we understand that type has two uses

Get type

Create a new type

I believe the first point, we all use it frequently, do we understand it?

What is the second point? Let's leave a suspense and then look down

99.99% of the creation methods of the class

Everyone, whether java or Python, understands that creating a class, class + class name OK

So you know what memory does when you name the class + class and then assign values or define instance methods


class MainClass:
  name='Uranus'

This method should be less than 1 cent higher than pass. But do you know what it does in memory?

First he creates a variable called MainClass python then opens up a block of memory space for creating a class named ClassMain Point the variable ClassMain to ClassMain Create a dictionary of dict in this method dict This dictionary points to 1 memory space where {name: Uranus} is stored

Is it what you think? Let's not talk about one kind or one kind. It's estimated that many people will say that I am nonsense … it doesn't matter

Introduces 0.01% of class creation methods

Just left a question, the second usage of type, to create a new type

What does it do? Let me first demonstrate a piece of code


def func():
  return 'is a function...'
#  The point is here 
TypeClass=type('MainClass1',(),{'name':'Uranus','func':func})
 
print(TypeClass.__class__)
# output: <class 'type'>
 
print(TypeClass)
# output: <class '__main__.MainClass1'>
 
print(TypeClass.__dict__)
# output: {'__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'MainClass1' objects>, '__dict__': <attribute '__dict__' of 'MainClass1' objects>, '__doc__': None, 'func': <function func at 0x00000000024DDEA0>, 'name': 'Uranus'}
 
print(TypeClass.name)
# output: Uranus
 
print(TypeClass.func())
# output: is a function...

Ok, now tell me what TypeClass is, is it a class?

What did I do in type, create class names, ignore the parent class, create class properties, create instance methods of the class?

Traditional class creation, knowledge will TypeClass this variable name setting and your class name MainClass 11 just like
Do you know the second method of type? Do you understand the process of creating classes? Would you before?

Thank you for reading and supporting this site.


Related articles: