SoFunction
Updated on 2024-11-18

python protoclasses, class creation process and methods in detail

Today we introduce you to class-related knowledge in python ......

Get the class name of the object

python is an object-oriented language, for everything connected to the object of python, we need to learn and understand some knowledge in-depth

First of all, we all know that to get the class corresponding to an object, you need to use class to retrieve it.

But can we use it this way if we just have a simple assignment statement? Let's take a 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'>

As we can see from the code above, whether we are simply assigning a value, or if we define a class and generate an object from that class, we can use __class__ to look up the methods corresponding to the object. We can use __class__ to look up the methods corresponding to the object. This should be understood by most people. Moving on.

Treating classes as objects in our eyes

We just got the class names str, int, and our customized MainClass by way of __class.

But if we continue to treat them as objects, who is the corresponding class? Let's print

print(int.__class__) # Or write it this way: 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 proto-class and is the class that creates other classes...

One has to ask, is TYPE an ancestor? There is no ancestor of the ancestor? You can verify it yourself, by following the method above

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

This is dead ah, TYPE this ancestor above no ancestor. Haha ....

Why type?

type is a function we often use, such as an object, we want to know what type it is, then we will use type(xxx)

So what exactly is type? Read 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 comment we understand that type has two uses

Get Type

Create new type

I believe the first point, which we all use regularly, is not understood.

What's the second point for? Let's leave it open. Let's move on.

Class 99.99% creation method

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

So you know exactly what you're doing in memory when you class+class name and then assign or define instance methods

class MainClass:
  name='Uranus'

This method is supposed to be less than a penny more upscale than pass in the brain-dead category. But do you know what it does in memory?

  • First he creates a variable, which is called MainClass
  • After that python opens up a block of memory space for creating a class called ClassMain
  • Point the variable ClassMain to ClassMain.
  • Create a dictionary of dicts in this method
  • The dictionary dict points to a memory space holding the {name:Uranus}

Is it what you think? Let's not say it's the same or not, I think a lot of people are going to say I'm making this up...never mind!

Introducing the 0.01% class creation method

I just left a question about the second use of type, which is used to create a new type

What does it do? Let me start by demonstrating a snippet of code

def func():
  return 'is a function...'
# Here's the point
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()
# output: Uranus
 
print(())
# output: is a function...

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

What am I doing in type, am I creating the class name, ignoring the parent class, creating the class attributes, creating the class instance methods?

In traditional class creation, Knowledge sets the variable TypeClass to be the same as your class name, MainClass1.
Do you know the second method of type? Do you understand how classes are created? Do you know how to create a class before?

Thank you all for reading and supporting me.