class-attributes

Python - function as class attribute becomes a bound method

我怕爱的太早我们不能终老 提交于 2019-12-10 00:58:41
问题 I noticed that if I define a class attribute equal to a function when I create an instance of that class the attribute becomes a bound method. Can someone explain me the reason of this behaviour? In [9]: def func(): ...: pass ...: In [10]: class A(object): ....: f = func ....: In [11]: a = A() In [12]: a.f Out[12]: <bound method A.func of <__main__.A object at 0x104add190>> In [13]: a.f() --------------------------------------------------------------------------- TypeError Traceback (most

Class attribute evaluation and generators

蹲街弑〆低调 提交于 2019-12-09 02:36:59
问题 How exactly does Python evaluate class attributes? I've stumbled across an interesting quirk (in Python 2.5.2) that I'd like explained. I have a class with some attributes that are defined in terms of other, previously defined attributes. When I try using a generator object, Python throws an error, but if I use a plain ordinary list comprehension, there's no problem. Here's the pared-down example. Note that the only difference is that Brie uses a generator expression, while Cheddar uses a

Python and object/class attrs - what's going on?

此生再无相见时 提交于 2019-12-08 05:37:55
问题 Can someone explain why Python does the following? >>> class Foo(object): ... bar = [] ... >>> a = Foo() >>> b = Foo() >>> a.bar.append(1) >>> b.bar [1] >>> a.bar = 1 >>> a.bar 1 >>> b.bar [1] >>> a.bar = [] >>> a.bar [] >>> b.bar [1] >>> del a.bar >>> a.bar [1] It's rather confusing! 回答1: As others have said the code as written creates a class variable rather than an instance variable. You need to assign in __init__ to create an instance variable. Hopefully this annotated copy of your code

Python and object/class attrs - what's going on?

心不动则不痛 提交于 2019-12-06 16:13:53
Can someone explain why Python does the following? >>> class Foo(object): ... bar = [] ... >>> a = Foo() >>> b = Foo() >>> a.bar.append(1) >>> b.bar [1] >>> a.bar = 1 >>> a.bar 1 >>> b.bar [1] >>> a.bar = [] >>> a.bar [] >>> b.bar [1] >>> del a.bar >>> a.bar [1] It's rather confusing! As others have said the code as written creates a class variable rather than an instance variable. You need to assign in __init__ to create an instance variable. Hopefully this annotated copy of your code is helpful in explaining what's going on at each stage: >>> class Foo(object): ... bar = [] # defines a class

Python - function as class attribute becomes a bound method

邮差的信 提交于 2019-12-04 23:12:51
I noticed that if I define a class attribute equal to a function when I create an instance of that class the attribute becomes a bound method. Can someone explain me the reason of this behaviour? In [9]: def func(): ...: pass ...: In [10]: class A(object): ....: f = func ....: In [11]: a = A() In [12]: a.f Out[12]: <bound method A.func of <__main__.A object at 0x104add190>> In [13]: a.f() --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-13-19134f1ad9a8> in <module>() ----> 1 a.f() global a.f = <bound method A

Can I mark a class as not my code so the debugger steps over it?

不想你离开。 提交于 2019-12-04 00:11:20
I have a utility class that has been thoroughly tested, and I do not want the VS debugger to step into any of its methods. I think I have heard of a way to mark something as not my code so that the Just My Code debugger setting causes the debugger to step over these method calls, but for the life of me I cannot recall what the class attribute is (nor can I successfully Google for it). I know that I could separate this class into its own assembly and build it in release mode to alleviate the issue, but I would like to step into some of the assembly (and I would like to keep this class where it

How to teach SpecFlow to add additional NUnit attributes to my test class

拜拜、爱过 提交于 2019-12-03 13:38:31
SpecFlow is great - and it helps us very much to do proper integration testing. One thing I was wondering is whether there's a way to tell SpecFlow to add additional NUnit attributes to the test class it creates in the feature code-behind file. Right now, my test class gets generated something like this: [System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "1.8.1.0")] [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [NUnit.Framework.TestFixtureAttribute()] [NUnit.Framework.DescriptionAttribute("Some action description here")] public partial class MySampleFeature {

Lazy class property decorator

谁都会走 提交于 2019-12-01 22:49:03
I have one django model which needs to do some processing referring the custom user model. I can't work with the class of this model at class loading time because the loading order of the classes is unknown. So I need to add some class attributes at runtime, at the moment I'm adding them in the __init__ or __new__ like: def __new__(cls, *args, **kwargs): # hack to avoid INSTALLED_APPS initialization conflicts. # get_user_model() can't be called from this module at class loading time, # so some class attributes must be added later. # Metaclasses could me more appropiate but I don't want to

Dictionary class attribute that refers to other class attributes in the definition

孤人 提交于 2019-11-29 16:01:31
While there are numerous ways around this, because of a personality fault I can't let it go until I understand the nature of the failure. Attempting: class OurFavAnimals(object): FAVE = 'THATS ONE OF OUR FAVORITES' NOTFAVE = 'NAH WE DONT CARE FOR THAT ONE' UNKNOWN = 'WHAT?' FAVES = defaultdict(lambda: UNKNOWN, {x:FAVE for x in ['dog', 'cat']}) FAVES['Crab'] = NOTFAVE Fails with: 3 NOTFAVE = 'NAH WE DONT CARE FOR THAT ONE' 4 UNKNOWN = 'WHAT?' ----> 5 FAVES = defaultdict(lambda: UNKNOWN, {x:FAVE for x in ['dog', 'cat']}) 6 FAVES['Crab'] = NOTFAVE NameError: global name 'FAVE' is not defined Why?

What's the simplest most elegant way to utilize a custom attribute

天涯浪子 提交于 2019-11-29 15:29:59
So a little confession, I've never written an attribute class. I understand they serve the purpose of decorating classes with flags or extra functionality possibly. Can someone give me a quick example of not just creating and applying an attribute to a class, but rather utilizing the attribute from another block of code. The only code samples I've ever seen to utilize any form of attributes was doing so with reflection, though I've always hoped there's a way of using them without reflection. Attributes are always used with reflection. They are baked into the metadata of the types during