class-attributes

Access base class attribute in derived class - in “class scope”

风流意气都作罢 提交于 2019-11-29 11:52:37
class Outer(object): class InnerBase(object): _var = {'foo', 'bar'} class Derived(InnerBase): _var = _var | {'baz'} # NameError: name '_var' is not defined _var = InnerBase._var | {'baz'} # name 'InnerBase' is not defined _var = Outer.InnerBase._var | {'baz'} # free variable 'Outer' # referenced before assignment in enclosing scope Moving _var in Outer does not help - moving it in module scope would work but defeats the purpose of having classes. So how to go about that ? EDIT: coming from Java so the scoping rules of classes are a head scratcher for me - a briefing would be appreciated. This

python: What happens when class attribute, instance attribute, and method all have the same name?

て烟熏妆下的殇ゞ 提交于 2019-11-28 18:41:29
How does python differentiate a class attribute, instance attribute, and method when the names are the same? class Exam(object): test = "class var" def __init__(self, n): self.test = n def test(self): print "method : ",self.test test_o = Exam("Fine") print dir(test_o) print Exam.test print test_o.test test_o.test() Output : ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'test'] <unbound

Dynamically create class attributes

旧街凉风 提交于 2019-11-28 10:56:41
I need to dynamically create class attributes from a DEFAULTS dictionary. defaults = { 'default_value1':True, 'default_value2':True, 'default_value3':True, } class Settings(object): default_value1 = some_complex_init_function(defaults[default_value1], ...) default_value2 = some_complex_init_function(defaults[default_value2], ...) default_value3 = some_complex_init_function(defaults[default_value3], ...) I could also achive this by having sth. like __init__ for class creation, in order to dynamically create these attributes from dictionary and save a lot of code and stupid work. How would you

Python class inheritance: AttributeError: '[SubClass]' object has no attribute 'xxx'

谁说我不能喝 提交于 2019-11-28 09:44:46
I have the following base class and subclass: class Event(object): def __init__(self, sr1=None, foobar=None): self.sr1 = sr1 self.foobar = foobar self.state = STATE_NON_EVENT # Event class wrappers to provide syntatic sugar class TypeTwoEvent(Event): def __init__(self, level=None): self.sr1 = level self.state = STATE_EVENT_TWO Further on in my code, I am inspecting an instance of a TypeTwoEvent class, checking for a field I know exists in the base class - I expected it to be defaulted to value None . However, my code raises the following exception: AttributeError: 'TypeTwoEvent' object has no

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

ぐ巨炮叔叔 提交于 2019-11-28 09:07:04
问题 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.

Access base class attribute in derived class - in “class scope”

独自空忆成欢 提交于 2019-11-28 05:08:35
问题 class Outer(object): class InnerBase(object): _var = {'foo', 'bar'} class Derived(InnerBase): _var = _var | {'baz'} # NameError: name '_var' is not defined _var = InnerBase._var | {'baz'} # name 'InnerBase' is not defined _var = Outer.InnerBase._var | {'baz'} # free variable 'Outer' # referenced before assignment in enclosing scope Moving _var in Outer does not help - moving it in module scope would work but defeats the purpose of having classes. So how to go about that ? EDIT: coming from

How to document class attributes in Python? [closed]

浪尽此生 提交于 2019-11-28 04:14:42
I'm writing a lightweight class whose attributes are intended to be publicly accessible, and only sometimes overridden in specific instantiations. There's no provision in the Python language for creating docstrings for class attributes, or any sort of attributes, for that matter. What is the accepted way, should there be one, to document these attributes? Currently I'm doing this sort of thing: class Albatross(object): """A bird with a flight speed exceeding that of an unladen swallow. Attributes: """ flight_speed = 691 __doc__ += """ flight_speed (691) The maximum speed that such a bird can

Real world use of custom .NET attributes

萝らか妹 提交于 2019-11-27 16:34:28
问题 What kind of things have you used custom .NET attributes for in the real world? I've read several articles about them, but I have never used custom attributes. I feel like I might be overlooking them when they could be useful. I am talking about attributes that you create, not ones that are already included in the framework. 回答1: I've used them "custom" attributes for validation (ie. marking a field to be validated with my own "credit card validation") and custom LinqToLucene analyzers I've

python: What happens when class attribute, instance attribute, and method all have the same name?

巧了我就是萌 提交于 2019-11-27 11:36:29
问题 How does python differentiate a class attribute, instance attribute, and method when the names are the same? class Exam(object): test = "class var" def __init__(self, n): self.test = n def test(self): print "method : ",self.test test_o = Exam("Fine") print dir(test_o) print Exam.test print test_o.test test_o.test() Output : ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '_

Dynamically create class attributes

浪子不回头ぞ 提交于 2019-11-27 05:51:48
问题 I need to dynamically create class attributes from a DEFAULTS dictionary. defaults = { 'default_value1':True, 'default_value2':True, 'default_value3':True, } class Settings(object): default_value1 = some_complex_init_function(defaults[default_value1], ...) default_value2 = some_complex_init_function(defaults[default_value2], ...) default_value3 = some_complex_init_function(defaults[default_value3], ...) I could also achive this by having sth. like __init__ for class creation, in order to