Skip to main contentdfsdf

    • class Delegate:  '''Handles a list of methods and functions  Usage:  d = Delegate()  d += function # Add function to end of delegate list  d(*args, **kw) # Call all functions, returns a list of results  d -= function # Removes last matching function from list  d -= object # Removes all methods of object from list  '''  def __init__(self):  self.__delegates = []   def __iadd__(self, callback):  self.__delegates.append(callback)  return self   def __isub__(self, callback):  # If callback is a class instance,  # remove all callbacks for that instance  self.__delegates = [ cb  for cb in self.__delegates  if getattr(cb, 'im_self', None) != callback]   # If callback is callable, remove the last  # matching callback  if callable(callback):  for i in range(len(self.__delegates)-1, -1, -1):  if self.__delegates[i] == callback:  del self.__delegates[i]  return self  return self   def __call__(self, *args, **kw):  return [ callback(*args, **kw)  for callback in self.__delegates]
    • class Event(property):  '''Class event notifier  Usage:  class C:  TheEvent = Event()  def OnTheEvent(self):  self.TheEvent(self, context)   instance = C()  instance.TheEvent += callback  instance.OnTheEvent()  instance.TheEvent -= callback  '''  def __init__(self):  self.attrName = attrName = "__Event_" + str(id(self))  def getEvent(subject):  if not hasattr(subject, attrName):   setattr(subject, attrName, Delegate())  return getattr(subject, attrName)  super(Event, self).__init__(getEvent)   def call(self, subject, *args, **kw):  if hasattr(subject, self.attrName):  getattr(subject, self.attrName)(subject, *args, **kw)

    2 more annotations...

  • Jul 09, 08

    modules in the same package can be imported without the long package prefixes!

    • Thus, the surround module can simply use import echo or from echo import echofilter.
    • When a generator function is called, the actual arguments are bound to  function-local formal argument names in the usual way, but no code in  the body of the function is executed.
      • important!

    • All this happens at the end of the class statement, after the body of the class (where methods and class variables are defined) has already been executed.
    • When a class statement is executed, the interpreter first determines the appropriate metaclass M, and then calls M(name, bases, dict).

    3 more annotations...

    • If __setattr__() wants to assign to an instance attribute, it  should not simply execute "self.name = value" -- this would cause a recursive call to itself. Instead, it should insert the value in the dictionary of instance attributes, e.g., "self.__dict__[name] = value". For new-style classes, rather than accessing the instance dictionary, it should call the base class method with the same name, for example, "object.__setattr__(self, name, value)".
    • which is the first place
    • the instance's class has an attribute by that name, the search continues with the class attributes

    3 more annotations...

  • Jul 15, 08

    Everything is an attribute, one way or another. Let's start simple: an object has a special attribute named __dict__, in which (references to) instance attributes - those you set with 'self.attr=val' - are stored. As I mentioned above, our object has another special attribute named __class__, that points to the class object - which, being an object, has itself a __dict__ attribute in which class attributes are stored, and a __class__ attribute pointing to the metaclass - the class of the class. Note BTW that Python functions being objects, the term 'attribute' include methods... (or more exactly, functions that will be turned into methods when looked up) Ok, now how is this used. <overly-simplified> When a name is looked up on an object (via the lookup operator '.'), it is first looked up in the object's __dict__. If this fails, it's then looked up in the class's __dict__, then in parent classes __dict__s. If the attribute object found is a callable, it is wrapped in a method object before. If the object is a descriptor (and has been found in a class __dict__), then the __get__ method of the descriptor is called.</overly-simplified> NB : a descriptor is an object that implements at least a __get__ method that takes an instance (the one on which the lookup occurs) and a class as params. Properties are descriptors. methods too. The __dict__ of a class is populated with the names and functions defined in the class: block. The __dict__ of an instance is populated with the attributes bound to the instance (usually inside method, and mostly inside the __init__() method, which is called just after object's creation) - with the exception that, if the name already refers to a descriptor having a __set__ method, then this will take precedence. All this is a 1000 feet high, overly simplified view of Python's object model - the lookup rules are a bit more complex (cf the __getattr__/__setattr__/__getattribute___ methods, and the complete definition of the dexcriptor protocol), and I didn't even talked about slots nor metacla

    • contains only
    • Observe that only __dict__ is searched,

    7 more annotations...

    • The implementation adds two special read-only attributes to class instance methods: m.im_self is the object on which the method operates, and m.im_func is the function implementing the method. Calling m(arg-1arg-2, ..., arg-n) is completely equivalent to calling m.im_func(m.im_self, arg-1arg-2, ..., arg-n).
1 - 10 of 10
20 items/page
List Comments (0)