python-datamodel

Multiply operator applied to list(data structure)

狂风中的少年 提交于 2019-11-26 14:45:58
问题 I'm reading How to think like a computer scientist which is an introductory text for "Python Programming". I want to clarify the behaviour of multiply operator ( * ) when applied to lists. Consider the function make_matrix def make_matrix(rows, columns): """ >>> make_matrix(4, 2) [[0, 0], [0, 0], [0, 0], [0, 0]] >>> m = make_matrix(4, 2) >>> m[1][1] = 7 >>> m [[0, 0], [0, 7], [0, 0], [0, 0]] """ return [[0] * columns] * rows The actual output is [[0, 7], [0, 7], [0, 7], [0, 7]] The correct

Python: Implementing slicing in __getitem__

痴心易碎 提交于 2019-11-26 14:16:01
I am trying to implement slice functionality for a class I am making that creates a vector representation. I have this code so far, which I believe will properly implement the slice but whenever I do a call like v[4] where v is a vector python returns an error about not having enough parameters. So I am trying to figure out how to define the getitem special method in my class to handle both plain indexes and slicing. def __getitem__(self, start, stop, step): index = start if stop == None: end = start + 1 else: end = stop if step == None: stride = 1 else: stride = step return self.__data[index

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

孤街醉人 提交于 2019-11-26 14:06:39
问题 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

Get class that defined method

女生的网名这么多〃 提交于 2019-11-26 12:18:58
How can I get the class that defined a method in Python? I'd want the following example to print " __main__.FooClass ": class FooClass: def foo_method(self): print "foo" class BarClass(FooClass): pass bar = BarClass() print get_class_that_defined_method(bar.foo_method) Alex Martelli import inspect def get_class_that_defined_method(meth): for cls in inspect.getmro(meth.im_class): if meth.__name__ in cls.__dict__: return cls return None Thanks Sr2222 for pointing out I was missing the point... Here's the corrected approach which is just like Alex's but does not require to import anything. I don

Python, should I implement __ne__() operator based on __eq__?

冷暖自知 提交于 2019-11-26 08:05:45
问题 I have a class where I want to override the __eq__() operator. It seems to make sense that I should override the __ne__() operator as well, but does it make sense to implement __ne__ based on __eq__ as such? class A: def __eq__(self, other): return self.value == other.value def __ne__(self, other): return not self.__eq__(other) Or is there something that I\'m missing with the way Python uses these operators that makes this not a good idea? 回答1: Yes, that's perfectly fine. In fact, the

Get fully qualified class name of an object in Python

99封情书 提交于 2019-11-26 04:44:39
问题 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? 回答1: 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

Python: Implementing slicing in __getitem__

不打扰是莪最后的温柔 提交于 2019-11-26 03:37:32
问题 I am trying to implement slice functionality for a class I am making that creates a vector representation. I have this code so far, which I believe will properly implement the slice but whenever I do a call like v[4] where v is a vector python returns an error about not having enough parameters. So I am trying to figure out how to define the getitem special method in my class to handle both plain indexes and slicing. def __getitem__(self, start, stop, step): index = start if stop == None: end

How is the &#39;is&#39; keyword implemented in Python?

爷,独闯天下 提交于 2019-11-26 01:39:47
问题 ... the is keyword that can be used for equality in strings. >>> s = \'str\' >>> s is \'str\' True >>> s is \'st\' False I tried both __is__() and __eq__() but they didn\'t work. >>> class MyString: ... def __init__(self): ... self.s = \'string\' ... def __is__(self, s): ... return self.s == s ... >>> >>> >>> m = MyString() >>> m is \'ss\' False >>> m is \'string\' # <--- Expected to work False >>> >>> class MyString: ... def __init__(self): ... self.s = \'string\' ... def __eq__(self, s): ..

Getting the class name of an instance?

孤街醉人 提交于 2019-11-26 00:45:38
问题 How do I find out a name of class that created an instance of an object in Python if the function I am doing this from is the base class of which the class of the instance has been derived? Was thinking maybe the inspect module might have helped me out here, but it doesn\'t seem to give me what I want. And short of parsing the __class__ member, I\'m not sure how to get at this information. 回答1: Have you tried the __name__ attribute of the class? ie type(x).__name__ will give you the name of

Is there a built-in function to print all the current properties and values of an object?

好久不见. 提交于 2019-11-26 00:38:53
问题 So what I\'m looking for here is something like PHP\'s print_r function. This is so I can debug my scripts by seeing what\'s the state of the object in question. 回答1: You are really mixing together two different things. Use dir(), vars() or the inspect module to get what you are interested in (I use __builtins__ as an example; you can use any object instead). >>> l = dir(__builtins__) >>> d = __builtins__.__dict__ Print that dictionary however fancy you like: >>> print l ['ArithmeticError',