Home >Backend Development >Python Tutorial >Basic syntax of object-oriented and classes in python

Basic syntax of object-oriented and classes in python

高洛峰
高洛峰Original
2017-03-03 13:48:351017browse

When I found out that I wanted to write object-oriented programming in python, I was hesitant and restless. I have been thinking: How should I climb this pit? Because there is a lot of object-oriented content in python, if you want to explain it thoroughly, it is best to re-learn the previous content using object-oriented thinking. This pit is so big. After much hesitation, let’s just pick up the important content. The insufficient content can only be supplemented by everyone.

Conventional statement, the version I am using is python2.7, there may be differences between versions.

Okay, before we start, let’s think about a question first and look at the code:

Basic syntax of object-oriented and classes in python

Why do I only create and assign a value to a? Use some method I haven't written yet?

Some friends may say: Because a is a string object at this time, of course you can use string methods. As for these methods, they are written in advance by python.

Okay, let’s change the question, why does python know it is a string object?

Before we talk about this issue in depth, remember one sentence: everything in Python is an object, and objects are created by classes.

So what is a class? I'll give you an example here.

One day, I was walking on the street and suddenly saw an enemy in front of me. At this time, I wanted to punch him, but the forces interacted with each other, and my fist would hurt if I hit him. Hey, at this time I found a stone on the side of the road. The size and shape were just right. Regardless of what the police officer thought, this was a good choice. Then I picked up the stone and performed some actions on my enemy that a good boy cannot imitate. Then I hid my merit and fame and called it a day and went home.

In this process, why do we know that the thing is a stone?

Because it has the appearance of stone!

Why do we know that rocks can hit people?

Because the stone is hard!

Why do we know that rocks are hard?

Because...well...my dad told me when I was little.

At this point we can draw a conclusion: we know what a thing is and what functions it has because we already have a concept of this thing in our minds. This concept may be known from elders, or it may be summarized by oneself after various experiments. Similar to our understanding of 'black holes', they are derived from various studies by scientists.

How to use the code to implement the example of hitting someone with a stone:

class Stone(object):  # 我创建一个叫Stone的类

  def attack(self):
    print '把头伸过来,我给你加个buff'

a = Stone()  # 我用类创建了一个对象,也称为类的实例化
a.attack()   # 我使用这个对象的方法

Basic syntax of object-oriented and classes in python

Good, this way we have accomplished what we need.

If we create other objects:



Basic syntax of object-oriented and classes in python

##Obviously python does not There is no attack method prepared for us, so we can't use it.

The so-called class is just an abstract definition, while the instance is a specific object. The difference between them is like the stone in my mind and the stone in my hand, only the latter is real. Of course, don’t talk to me about idealism or anything like that here.

Seeing this, it should be clearer about the relationship between classes and objects. Some students may ask: All classes in python are written by us in advance. Is there a way to let python automatically generate classes and then use these classes at specific times? Congratulations, classmate, you may have touched the threshold of artificial intelligence. If it can be realized, wouldn't it be similar to our human learning ability? Well, this is just an idea of ​​mine. I don’t know how to realize real artificial intelligence. I haven’t studied it at such a high level yet. However, as a programmer, being able to program artificial intelligence may be a lifelong pursuit. Come on.

After talking about what classes and instantiation are, let’s take a look at the basic syntax of classes.

First of all, like def for defining functions, class is the keyword for defining a class.

紧接着的是类名,这个可以自定义,同样的,不能和python的内置关键字冲突。另外,建议避开python的内建类型,例如 str、int之类的名字。规范的命名应该遵从“驼峰命名法”,例如: MyClass 这里的命名,每个单词的首字母大写。

然后是一个括号,里面的参数是用于继承的,一般继承于 object,表示一个新式类。另外,你可能见过没有括号的写法,这是经典类的写法。

示例:

class NewClass(object):
  pass

class OldClass:
  pass

New = NewClass()  # 创建一个新式类的实例
Old = OldClass()  # 创建一个经典类的实例

这就是类的基本语法,当然这样还是不够的,但是在更深入之前,我想先讲一个新旧式类的差别。

在这里,我们先打印一下两个变量的类型:

print type(New)
print type(Old)

Basic syntax of object-oriented and classes in python

可以看下两者的输出是不同的。

在早于python2.2的版本时,只有经典类这一种写法,当时,类和类型没有合并。

类是类对象,实例是实例对象,这两个对象之间没有任何关系。

这句话是什么意思?看代码:

print type(OldClass)
print type(Old)

Basic syntax of object-oriented and classes in python

我们可以看见其输出很含糊,经典类属于类对象,无论是哪个类,都统一为“类”类型,实例属于实例类型,却不知道其是由哪个类创建的,所以的实例都统一为“实例”类型。也就是说当时的类型用 classobj 和 instance 代表了所以的类和实例,无论你是哪个类,又或是哪个类创建的实例。

这样的信息实在太少,而类和类型之间非常混乱。为了解决这种情况,在 python2.2 中引入了新式类,并进行了类和类型的同统一。

print type(NewClass)
print type(New)

Basic syntax of object-oriented and classes in python

类的类型是 type?type 返回的对象还能像类一样创新新对象?

总结的来说:在新式类中,所以的类对象都是 type 的实例。而不同的类对象有能创建出其对应的实例。

class NewClass(object):
  def __init__(self, val):
    self.val = val

New = NewClass(123)  
b = type(New)(321) # 对实例来说type返回的是类对象,我又可以用类对象来和创建新的实例
print b.val

Basic syntax of object-oriented and classes in python

构造器方法

一般可以理解类中的函数就是方法,而方法分为:实例方法,只有实例化后才能调用的,其第一个参数一般为 self,代表实例本身;类方法,其第一个参数为 cls,代表类本身;还有静态方法,就是个普通函数,没有要求参数。

1. __init__(self [,arg1,....]):

当类被调用进行实例化的时候,python会自动调用类里面的构造函数(如果有的话),在构造函数中,可以进行各种初始化的操作,最常见的就是上面的进行实例的属性的创建。

python 在示例化的时候,会检查其实行了 __init__ 方法了没有,如果没有则不对实例进行任何操作,然后返回对象。如果实行了这个方法,则自动调用这个方法,并自动将 self 传进行,也就是说我们在实例化进行传参的时候,将不用理会 self,直接传给后面的参数。

讲到属性,就必须要提一下什么是属性。属性这个对象其实更像一个变量,大多数对象都可以有属性(不包括python的内置类型),例如函数。

def Test():
  pass

Test.a = 123
print Test.a

Basic syntax of object-oriented and classes in python

因为函数也是一个对象。

属性在类中,就是一个变量,例如:

class NewClass(object):
  a = 123

print NewClass.a

Basic syntax of object-oriented and classes in python

当然,因为 python 的特性,我们可以在运作中为某个对象添加属性,而不用一开始就在类中写定。

注意,这个方法应该返回 None,也就是说我们一般不用 return 任何对象,让它默认返回就行了。

2. __new__(cls [,arg1,....]):

这也是一个构造器方法,它是一个类方法,一般在对 python 的不可变数据类型进行继承扩展的时候用的比较多。

某处拿来的代码示例:

class RoundFloat(float):
  def __new__(cls, val):
    return super(RoundFloat, cls).__new__(cls, round(val, 2))

a = RoundFloat(3.14159)
print a

Basic syntax of object-oriented and classes in python

解构器方法

__del__(self [,arg1,....])

这个方法将会在对象所以的引用被清除后才执行,例如:

class Test(object):

  def __del__(self):
    print '我被干掉了,兄弟们为我报仇!'

a = Test() # 创建了一个对象
b = a  # b又引用了a
c = b  # c又引用了b,现在 a 所指向的对象有3次引用,相当有三条命
del a  # 干掉一条命
del b  # 又干掉
del c  # 听说你有3条命?全部干掉!

Basic syntax of object-oriented and classes in python

注意,这里只输出了一次,也就是说到了最后才删除完毕。这里要注意一下几点:

1.调用 del 并不意味着完成删除某个对象,只是减少引用。

2.如果你有一个循环引用或其它的原因,让一个实例的引用逗留不去,该对象的__del__()可能永远不会被执行。

3.__del__()未捕获的异常会被忽略掉(因为一些在__del__()用到的变量或许已经被删除了)。 不要在__del__()中干与实例没任何关系的事情。

4.一般情况下并不用实现这个方法,因为这样有一定的风险。

5.如果你定义了__del__,并且实例是某个循环的一部分,垃圾回收器将不会终止这个循环— —你需要自已显式调用 del。

6.如果继承了父类,且父类中也有解构器,要记得调用。否则可能会有某些在父类中的清理方法没有调用到,出现以下无法预料的错误。

以上这篇浅谈Basic syntax of object-oriented and classes in python就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持PHP中文网。

更多Basic syntax of object-oriented and classes in python相关文章请关注PHP中文网!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn