0

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.

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?

2
  • 1
    What do you mean by difference? SourcePort is derived from Base and Tutorial is inherited from object. Commented May 4, 2013 at 6:33
  • Here's the documentation for object. You might find this explanation (and the link at the bottom of the paragraph) useful. Commented May 4, 2013 at 6:39

2 Answers 2

2

You seem to be using SQLAlchemy in the first case. 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.

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).

So that strange Base class, or some of its parents, has a metaclass bound to it. After the class SourcePort... statement is executed, the contents of SourcePort class are modified by the metaclass. 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.

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. They share a few traits, but have a sea of differences. Just learn about Python classes as if these were a totally alien concept.

Sign up to request clarification or add additional context in comments.

Comments

2

In Python 2.2 new style classes were introduced which should have object as a parent. Without having object as a (grand)parent it'll be an old style class. In Python 3 all classes are "new".

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.