python-datamodel

Remove elements as you traverse a list in Python [duplicate]

爱⌒轻易说出口 提交于 2019-11-28 07:01:28
This question already has an answer here: How to remove items from a list while iterating? 26 answers In Java I can do by using an Iterator and then using the .remove() method of the iterator to remove the last element returned by the iterator, like this: import java.util.*; public class ConcurrentMod { public static void main(String[] args) { List<String> colors = new ArrayList<String>(Arrays.asList("red", "green", "blue", "purple")); for (Iterator<String> it = colors.iterator(); it.hasNext(); ) { String color = it.next(); System.out.println(color); if (color.equals("green")) it.remove(); }

How Do I Perform Introspection on an Object in Python 2.x?

痴心易碎 提交于 2019-11-27 21:55:54
I'm using Python 2.x and I have an object I'm summoning from the aether; the documentation on it is not particularly clear. I would like to be able to get a list of properties for that object and the type of each property. Similarly, I'd like to get a list of methods for that object, as well, plus any other information I could find on that method, such as number of arguments and their respective types. I have a feeling that I am simply missing the correct jargon in my Google searches. Not that I want to derail with specifics, but it's Active Directory, so that's always fun. Well ... Your first

Prevent creating new attributes outside __init__

 ̄綄美尐妖づ 提交于 2019-11-27 17:07:55
I want to be able to create a class (in Python) that once initialized with __init__ , does not accept new attributes, but accepts modifications of existing attributes. There's several hack-ish ways I can see to do this, for example having a __setattr__ method such as def __setattr__(self, attribute, value): if not attribute in self.__dict__: print "Cannot set %s" % attribute else: self.__dict__[attribute] = value and then editing __dict__ directly inside __init__ , but I was wondering if there is a 'proper' way to do this? I wouldn't use __dict__ directly, but you can add a function to

How python attribute lookup process works?

回眸只為那壹抹淺笑 提交于 2019-11-27 11:37:08
问题 When i say "python attribute lookup proccess" i mean: what python does when you write x.foo?? Searching the web i didn't found to much docs about this, one of the best papers i found resumed the proccess to the following steps (you can see the full article here) If attrname is a special (i.e. Python-provided) attribute for objectname, return it. Check objectname.__class__.__dict__ for attrname. If it exists and is a data-descriptor, return the descriptor result. Search all bases of objectname

Schrödinger's variable: the __class__ cell magically appears if you're checking for its presence?

瘦欲@ 提交于 2019-11-27 08:17:30
There's a surprise here: >>> class B: ... print(locals()) ... def foo(self): ... print(locals()) ... print(__class__ in locals().values()) ... {'__module__': '__main__', '__qualname__': 'B'} >>> B().foo() {'__class__': <class '__main__.B'>, 'self': <__main__.B object at 0x7fffe916b4a8>} True It seems like the mere mention of __class__ is explicitly checked by the parser? Otherwise we should get something like NameError: name '__class__' is not defined Indeed, if you modify to only check the key instead, i.e. check for '__class__' in locals() , then we only have self in scope as expected. How

Remove elements as you traverse a list in Python [duplicate]

天大地大妈咪最大 提交于 2019-11-27 05:43:38
问题 This question already has answers here : How to remove items from a list while iterating? (28 answers) Closed 4 years ago . In Java I can do by using an Iterator and then using the .remove() method of the iterator to remove the last element returned by the iterator, like this: import java.util.*; public class ConcurrentMod { public static void main(String[] args) { List<String> colors = new ArrayList<String>(Arrays.asList("red", "green", "blue", "purple")); for (Iterator<String> it = colors

How Do I Perform Introspection on an Object in Python 2.x?

南楼画角 提交于 2019-11-26 23:06:31
问题 I'm using Python 2.x and I have an object I'm summoning from the aether; the documentation on it is not particularly clear. I would like to be able to get a list of properties for that object and the type of each property. Similarly, I'd like to get a list of methods for that object, as well, plus any other information I could find on that method, such as number of arguments and their respective types. I have a feeling that I am simply missing the correct jargon in my Google searches. Not

Is everything greater than None? [duplicate]

南楼画角 提交于 2019-11-26 20:38:55
This question already has an answer here: A number smaller than negative infinity in python? 1 answer Is there a Python built-in datatype, besides None , for which: >>> not foo > None True where foo is a value of that type? How about Python 3? John Feminella None is always less than any datatype in Python 2 (see object.c ). In Python 3, this was changed; now doing comparisons on things without a sensible natural ordering results in a TypeError . From the 3.0 "what's new" updates : Python 3.0 has simplified the rules for ordering comparisons: The ordering comparison operators ( < , <= , >= , >

Get fully qualified class name of an object in Python

放肆的年华 提交于 2019-11-26 18:46:40
For logging purposes I want to retrieve the fully qualified class name of a Python object. (With fully qualified I mean the class name including the package and module name.) I know about x.__class__.__name__ , but is there a simple method to get the package and module? With the following program #! /usr/bin/env python import foo def fullname(o): # o.__module__ + "." + o.__class__.__qualname__ is an example in # this context of H.L. Mencken's "neat, plausible, and wrong." # Python makes no guarantees as to whether the __module__ special # attribute is defined, so we take a more circumspect

Prevent creating new attributes outside __init__

…衆ロ難τιáo~ 提交于 2019-11-26 17:57:01
问题 I want to be able to create a class (in Python) that once initialized with __init__ , does not accept new attributes, but accepts modifications of existing attributes. There's several hack-ish ways I can see to do this, for example having a __setattr__ method such as def __setattr__(self, attribute, value): if not attribute in self.__dict__: print "Cannot set %s" % attribute else: self.__dict__[attribute] = value and then editing __dict__ directly inside __init__ , but I was wondering if