python Black Hat Programming 2.9 object-oriented programming
I personally think that the development of computer language, there are two directions, one is from low to high development process, in this process, the language of thinking and problem-solving approach is hardware-oriented. The hardware is essentially processing signals, on this basis, we give the hardware a certain "logical thinking" ability, in order to facilitate the hardware to help us do things, abstract out the concept of instruction, and then the emergence of assembly language, and then have Pascal and C such a standard structure of language. Language all the way up, are rooted in the instructions, rooted in the instruction means that the process and data represent everything, the change of data becomes the fundamental of our expression and abstraction of the world. There is no denying that everything in the universe is constantly changing, but the fate has several, the change has the law, the more we focus on the change, the more difficult we escape the fate.
In the years before or after the von Neumann system became authoritative, computer science thought was derived from thinking about mathematics. Mathematicians think that mathematical language can perfectly describe the world, mathematics-based programming language is a noble, but purely mathematical language is not used, because our machine is too low, so another programming language development direction, from the mathematical language to machine language from the top down development. The purely functional language is representative of this, such as Lisp, and Microsoft's F # is the "half-blood" representation.
Understanding the world from a machine's point of view, or a purely mathematical way of understanding the world, does not represent a person's perspective and way of thinking. In the two opposite development, there are a lot of convergence. Computers, of course, are far from understanding people's ways of thinking, unless they are turned into computational units. Scientists have been working in this direction for a long while, and the "object-oriented" ideas and methods in the field of programming languages have been widely accepted. Things are constantly changing, humans in the change to find a relatively static time and space to think about the world, to describe the world, words, paintings are languages, all need to present in the stillness. Life respects and manifests itself, recognizes the individual, and so the world has the concept of things. In the stillness, if it is just thinking about the data, then it is chaos, observing the individual has meaning, only flesh and blood. Everything in the world is the object of things.
The so-called object-oriented is to describe the independent affairs in your eyes that can be thought or abstracted, first it is a whole, then we dismember it, and finally we re-put it into the change to observe the behavior. The more thorough the abstraction, the more we can discover the commonality of many things, and hence the classification. In the change, things and things will inevitably produce impact, so there is a relationship.
The angle of view has changed, the way of describing things and behaviors is bound to change, produce new expression methods, new techniques, but also have new problems and challenges, of course, will produce new solutions to the problem, these are the basic object-oriented methods, design patterns, architectural experience, and so on.
Pull so many chatted, below we enter the object-oriented world of Python, of course, will not be nuanced, simple overview of the main.
2.9.1 Classes and objects
In Python, we use the Class keyword to define a kind of thing, a class is an abstract description, not a real existence, it needs to be initialized to produce a real thing that we call an object. In the programming process, owning the behavior and data is an object, not a class.
Here we declare a simple class:
#!/usr/bin/python
Class Person:
Pass # an empty block
p = person ()
Print P
We created a new class using the class statement followed by the classes name. This is followed by a indented block of statements to form the class body. In this example, we use a blank block, which is represented by a pass statement.
Next, we use the class name followed by a pair of parentheses to create an object/instance. To verify, we simply printed out the type of the variable. It tells us that we have an instance of the person class in the __main__ module. The results are as follows:
Figure 2
You may have noticed that the storage object's computer memory address is also printed out. This address will be a different value on your computer, because Python can store objects in any empty space.
Methods for 2.9.2 Objects
Method represents behavior in the real world, a simple example is as follows:
Class Person:
def sayhi (self):
print ' Hello, how is it? '
p = person ()
P.sayhi ()
Here we need to note that the Sayhi method does not have any parameters, but still self,self represents the object itself when the function is defined, equivalent to the self pointer in C + + and the This reference in Java, C #. After that, we call the method of the object by adding a dot to the object name. The results of the operation are as follows:
Figure 3
2.9.3 constructor function
When we use classes to construct an object, we usually construct the expense to initialize the key properties of the object itself. For example, to initialize a person, if we think age and name is the key attribute, when we instantiate this person, we need to pass the value of the relevant attribute in. Above we know that the time of the object is the class name parentheses, in fact, this parenthesis called a built-in method, which we call the constructor, it is the function returned the initialized object itself.
Python's constructor is named __init__, and we can customize the type and number of parameters passed in.
#!/usr/bin/python
Class Person:
def __init__ (self, name):
Self.name = Name
def sayhi (self):
print ' Hello, my name is ', Self.name
p = person (' Swaroop ')
P.sayhi ()
Here, we define the __init__ method to take a parameter name (as well as the normal argument self). In this __init__, we just create a new field name. Most importantly, we do not specifically call the __init__ method, but when creating a new instance of a class, include the parameters in parentheses followed by the class name, which is passed to the __init__ method.
Now, we are able to use the Self.name variable in our method. This has been verified in the Sayhi method.
Figure 4
2.9.4 variable
Variables represent properties and data. There are two types of variables in Python, class variables and object variables. Class variables are global and are shared for each object, and the object's variables are instantiated, with each object being unaffected. Here's a code to compare:
#!/usr/bin/python
Class Person:
"' represents a person. '
Population = 0
def __init__ (self, name):
"' Initializes the person ' s data. '
Self.name = Name
print ' (Initializing%s) '% Self.name
# When this is created, he/she
# adds to the population
Person.population + = 1
def __del__ (self):
"I am dying."
print '%s says bye. '% self.name
Person.population-= 1
If person.population = = 0:
print ' I am the last one. '
Else
print ' There is still%d people left. '% person.population
def sayhi (self):
"' greeting by the person.
Really, that's all it does.
print ' Hi, my name is%s. '% Self.name
def howmany (self):
"Prints the current population."
if person.population = = 1:
print ' I am the only person here. '
Else
print ' We have%d persons here. '% person.population
Swaroop = person (' Swaroop ')
Swaroop.sayhi ()
Swaroop.howmany ()
Kalam = person (' Abdul kalam ')
Kalam.sayhi ()
Kalam.howmany ()
Swaroop.sayhi ()
Swaroop.howmany ()
This example is a little bit long, but it helps to illustrate the nature of the variables of classes and objects. Here, population belongs to the person class and is therefore a variable of a class. The name variable belongs to the object (it uses the self assignment) and is therefore a variable of the object.
You can see that the __init__ method initializes the person instance with a name. In this method, we let population increase by 1, which is because we have added a person. It is also possible to find that the value of Self.name is specified for each object, which indicates the nature of the variable that it acts as an object.
In this program, we also see that docstring is also useful for classes and methods. We can use person.__doc__ and person.sayhi.__doc__ to access the document strings of classes and methods separately at run time.
Just like the __init__ method, there is a special method __del__, which is called when the object dies. When the object dies, the object is no longer in use, and the memory it consumes is returned to the system for its use. In this method, we simply reduce the person.population by 1.
The __del__ method runs when the object is no longer in use, but it is difficult to guarantee when the method will run. If you want to indicate its operation, you have to use the DEL statement, as we used in the previous example.
It is important to note that:
All class members (including data members) in Python are public by default.
L If you use a data member name with a double-underscore prefix such as __privatevar,python's name management system, it effectively takes it as a private variable.
L There is a convention that if a variable is only meant to be used in a class or object, it should be prefixed with a single underscore. Other names will be public and can be used by other classes/objects. Remember that this is only a convention, not what Python requires (unlike a double underscore prefix).
Also, note that the __del__ method is similar to the concept of destructors in other languages.
2.9.5 inheritance
Inheriting this concept is often explained by the relationship between the father and the son, which I think is misleading for beginners. What is the inheritance of the father and the son in human society? is a property, is actually a gift. Object-oriented inheritance, is actually not from the real world, but to solve the problem of object-oriented programming in the generation, to solve the problem of code reuse. For example, human beings have many similarities, noses and eyes. But humans are also sub-subspecies, from the color of the racial Division, Geographical division of the population. Then when defining the yellow and black people, it is inevitable to redefine a lot of attributes and behavior, so there is the concept of inheritance, put the public content into the person class, and then Asiaperson inherit the person class, it naturally has the person class definition of content, this idea, In fact, a different word may be better understood-cloning. The concept is not tangled, understanding is good, the following look at the code:
#!/usr/bin/python
Class Schoolmember:
"' represents any school member."
def __init__ (self, Name, age):
Self.name = Name
Self.age = Age
print ' (Initialized schoolmember:%s) '% Self.name
def tell (self):
"Tell my details."
print ' Name: '%s ' Age: '%s ' '% (Self.name, self.age),
Class Teacher (schoolmember):
"' Represents a teacher."
def __init__ (self, name, age, salary):
Schoolmember.__init__ (self, name, age)
Self.salary = Salary
print ' (Initialized Teacher:%s) '% Self.name
def tell (self):
Schoolmember.tell (self)
print ' Salary: '%d ' '% self.salary
Class Student (schoolmember):
"' represents a student."
def __init__ (self, name, age, marks):
schoolmember.__init__ (self, name, age)
Self.marks = Marks
print ' (Initialized Student:%s) '% Self.name
def tell (self):
Schoolmember.tell (self)
print ' Marks: '%d ' '% self.marks
t = Teacher (' Mrs. Shrividya ', 40, 30000)
s = Student (' Swaroop ', 22, 75)
Print # Prints a blank line
Members = [T, S]
For member in Members:
Member.tell () # Works for both Teachers and Students
To use inheritance, we take the name of the base class as a tuple following the class name when the class is defined. Then we notice that the __init__ method of the base class is specifically called with the self variable, so that we can initialize the base class part of the object. This is important--python does not automatically call the constructor of the base class. The constructor that calls the base class needs to be called using the base class name, and the self of the child class is passed in.
Note that when we use the Tell method of the Schoolmember class, we take the instance of teacher and student only as an instance of Schoolmember.
Also, in this example, we call the Tell method of the subtype, not the Tell method of the Schoolmember class. It can be understood that Python is always looking for a method of the corresponding type first, in this case. If it cannot find the corresponding method in the export class, it begins to look up one by one in the base class.
Python supports multiple inheritance and can inherit multiple base classes at the same time.
2.9.6 Summary
Python is a highly object-oriented language, and we just have a few basic concepts, everything is the object of the idea, but also make advanced Python programming more interesting.
At the beginning of the next section, we go into the black Hat programming phase, first to discuss the host-to-network layer of typical attacks.
Section 3.0 , "network interface layer attack basic knowledge " has been in the first release of the subscription number, impatient students into the subscription number (QR code below), from the menu "network Security", "Python Black Hat Programming" to enter.
To see the full range of tutorials, follow my subscription number (xuanhun521, QR code below) and reply to "Python". Question discussion please add QQ Group: Hacking (1 group): 303242737 Hacking (2 group): 147098303.
Python Black Hat Programming 2.9 object-oriented programming