简体   繁体   English

python中的类定义语法

[英]Class definition syntax in python

I am very new to python.I am working with some python code.I am trying to map the python object oriented concepts to those of C++ which I think is a good way to learn.I can across two types of class definitions. 我对python非常陌生,正在使用一些python代码,正在尝试将python面向对象的概念映射到C ++的概念,我认为这是学习的一种好方法。我可以跨两种类型的类定义。

class SourcetoPort(Base):
    """"""
    __tablename__ = 'source_to_port'
    id = Column(Integer, primary_key=True)
    port_no        = Column(Integer)
    src_address    = Column(String)

    #----------------------------------------------------------------------
    def __init__(self, src_address,port_no):
        """"""
        self.src_address = src_address    
    self.port_no     = port_no

and the second one. 第二个。

class Tutorial (object):
  def __init__ (self, connection):
    print "calling Tutorial __init__"
    self.connection = connection
    connection.addListeners(self)
    self.mac_to_port = {} 
    self.matrix={} 

I want to know what is the difference between the Base in SourcetoPort and object in Tutorial? 我想知道SourcetoPort中的Base和Tutorial中的对象之间有什么区别?

In Python 2.2 new style classes were introduced which should have object as a parent. 在Python 2.2中,引入了新样式类,这些类应具有object作为父object Without having object as a (grand)parent it'll be an old style class. 如果没有object作为(祖父母)父母,那么它将是一个旧样式类。 In Python 3 all classes are "new". 在Python 3中,所有类都是“新的”。

Inheritance from the object gives many nice features including descriptors, properties etc. Even if you're not going to use them, it's good idea to inherit object anyway. object继承提供了许多不错的功能,包括描述符,属性等。即使您不打算使用它们,还是要继承object是一个好主意。

You seem to be using SQLAlchemy in the first case. 您似乎在第一种情况下使用SQLAlchemy。 You definitely could not miss the difference in declaration (or, rather, execution). 您绝对不能错过声明(或执行)上的差异。

Beside the fact that Python classes are rather different from classes in static languages , your SourcePort class depends on a metaclass. 除了Python类与静态语言类完全不同之外 ,您的SourcePort类还取决于元类。

A metaclass is essentially a function that can alter or dynamically generate class contents. 元类本质上是一个可以更改或动态生成类内容的函数。 It's somehow reminiscent of C++ templates, but acting at runtime (in Python, everything happens at runtime). 它以某种方式让人联想到C ++模板,但在运行时执行操作(在Python中,所有操作都在运行时发生)。

So that strange Base class, or some of its parents, has a metaclass bound to it. 这样奇怪的Base类或它的某些父类就绑定了一个元类。 After the class SourcePort... statement is executed, the contents of SourcePort class are modified by the metaclass. 执行class SourcePort...语句之后,元类将修改SourcePort类的内容。 The metaclass reads the initial attributes that explain table name, columns, etc, and adds to SourcePort various methods to access database fields by name as if they were SourcePort 's fields, getters that might lazily load column contents (if initially declared so), setters that change the 'dirty' state of a SourcePort instance, all the mechanics that binds ORM objects to database sessions, etc. 元类读取解释表名,列等的初始属性,并向SourcePort添加各种方法来按名称访问数据库字段,就好像它们是SourcePort的字段一样,getter可能会延迟加载列内容(如果最初声明是这样),设置程序,更改SourcePort实例的“脏”状态,将ORM对象绑定到数据库会话的所有机制,等等。

So yes, there's a serious difference. 是的,两者之间存在严重差异。

A bit of unsolicited advice: to better understand Python classes, stop trying to make analogies to C++ classes. 一些不请自来的建议:为了更好地理解Python类,请停止尝试与C ++类进行类比。 They share a few traits, but have a sea of differences. 它们具有一些特征,但差异很大。 Just learn about Python classes as if these were a totally alien concept. 只需了解Python类,就好像它们是一个完全陌生的概念一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM