power of explicit self?
I'm looking for an explanation of how explicit self is implimented and what features are only possible because of, or are greatly improved, because of it. I've always liked explicit self and am looking for the computer science behind it, so that I can explain the benefits that I see. I'm also interested in the files/lines of the python source that shows how explicit self is implemented if anyone can point out where that takes place. all help welcome -- http://mail.python.org/mailman/listinfo/python-list
Re: power of explicit self?
> It's not implemented in the compiler. There's a place in the runtime > for invoking a method where the object is inserted at the beginning > of the parameter list. IIRC, that's done by wrapping the function > object. This is the source of Objects/methodobject.c it look like this is where self is added to the argument list, but I'll have to do some more digging. thanks for the tip. 50 PyObject * 51 PyCFunction_GetSelf(PyObject *op) 52 { 53 if (!PyCFunction_Check(op)) { 54 PyErr_BadInternalCall(); 55 return NULL; 56 } 57 return ((PyCFunctionObject *)op) -> m_self; 58 } 69 70 PyObject * 71 PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) 72 { ... 75 PyObject *self = PyCFunction_GET_SELF(func); ... 78 switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) { 79 case METH_VARARGS: 80 if (kw == NULL || PyDict_Size(kw) == 0) 81 return (*meth)(self, arg); 82 break; 83 case METH_VARARGS | METH_KEYWORDS: ... 126 } -- http://mail.python.org/mailman/listinfo/python-list
Re: insert unique data in a list
On Dec 13, 11:37 am, mattia wrote: > How can I insert non-duplicate data in a list? I mean, is there a > particular option in the creation of a list that permit me not to use > something like: > def append_unique(l, val): > if val not in l: > l.append(val) > > Thanks, > Mattia You could also define a custom object that manages a custom ordered set class unique_set(object): def __init__(self,list): self.list = list def __add___(self,val): if val not in self.list self.list.append(val) return True return False >>> unique_list = unique_set(['a','b','c']) >>> unique_list.list ['a', 'b', 'c'] >>> unique_list + 'd' True >>> unique_list + 'e' True >>> unique_list + 'd' False >>> unique_list.list ['a', 'b', 'c', 'd', 'e'] >>> I've used this on a few projects, it makes for wonderfully clean code, because you can look at your program as an overview without all the arithmetic behind it. hope it helps -- http://mail.python.org/mailman/listinfo/python-list
Re: insert unique data in a list
> Also, I'm not sure I like your abuse of the + operator to modify the > object in place and return a flag. It is an API not shared by (as far as > I can see) any other data type in Python. I agree it couuld be more consisten with other object apis, I also think that if every api has to conform to every other api nothing will ever get done. Heres a slightly more familiar version, it returns the value added or none to conform with other APIs. class unique_set(object): def __init__(self,list): self.list = list def __add___(self,val): if val not in self.list self.list.append(val) return val return None >>> unique_list = unique_set(['a','b','c']) >>> unique_list.list ['a', 'b', 'c'] >>> unique_list + 'd' 'd' then a test opperand to verify if a value was inserted could be if unique_list + 'd': # done some stuff but it could also be used in cases like this unique_added = unique_list + 'd' or 'not added' -- http://mail.python.org/mailman/listinfo/python-list