Class definition syntax in python -


i new python.i working python code.i trying map python object oriented concepts of c++ think way learn.i can across 2 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 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 know difference between base in sourcetoport , object in tutorial?

you seem using sqlalchemy in first case. not miss difference in declaration (or, rather, execution).

beside fact python classes rather different classes in static languages, sourceport class depends on metaclass.

a metaclass function can alter or dynamically generate class contents. it's somehow reminiscent of c++ templates, acting @ runtime (in python, happens @ runtime).

so strange base class, or of parents, has metaclass bound it. after class sourceport... statement executed, contents of sourceport class modified metaclass. metaclass reads initial attributes explain table name, columns, etc, , adds sourceport various methods access database fields name if sourceport's fields, getters might lazily load column contents (if declared so), setters change 'dirty' state of sourceport instance, mechanics binds orm objects database sessions, etc.

so yes, there's serious difference.

a bit of unsolicited advice: better understand python classes, stop trying make analogies c++ classes. share few traits, have sea of differences. learn python classes if these totally alien concept.


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -