This is a very contrived example as it's not easy to explain the context in which I have ended up implementing this solution. However, if anyone can answer why this particular peculiarity happens, I'd be grateful.
The example:
class A(dict):
def __init__(self):
self['a'] = 'success'
def __getitem__(self, name):
pri...
Pointing the class method at the instance method is clearly causing problems:
class A(dict):
def __getitem__(self, name):
return dict.__getitem__(self, name)
class B(object):
def __init__(self):
self.a = A()
B.__getitem__ = self.a.__getitem__
b1 = B()
b1.a['a'] = 5
b2 = B()
b2.a['b'] = 10
c = b1['a']
d = ...
When defining class attributes through "calculated" names, as in:
class C(object):
for name in (....):
exec("%s = ..." % (name,...))
is there a different way of handling the numerous attribute definitions than by using an exec? getattr(C, name) does not work because C is not defined, during class construction...
...
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.
EDIT
I am talking about attributes that you create, not ones that are already included in the framework.
...
On some occasions (e.g. suppress warning), VS generates class attributes. The problem I'm facing is about the way these attributes are added.
When advising Visual Studio to suppress the warning w1 and the warning w2, I expect this result:
[SuppressMessage("w1")]
[SuppressMessage("w2")]
public class Foo
Bit actually VS combines the at...
I am working on a CMS that generates CSS "style='xyz'" statements from user input.
The user input will be validated but as an additional safeguard, I want to check the validity of the values on generation of the CSS code.
If an invalid value is encountered - e.g. a relative width ("50%") where only absolute values are allowed due to lay...
Attributes are awesome. But is it possible to create a C# attribute class that, when tagged, makes your application minimize to the system tray?
Technically, the attribute would need to be placed on the class of the main form. Once the user clicks on the X button, that form shouldn't terminate, but should minimize to the taskbar. The ic...
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 c...
Hi, I would like to pass certain data from an instance of an object to its attribute, and I have troubles inderstanding how to implement it. Here's a sample:
[AuthenticateAttribute]
public class MyController: Controller
{
UserInfo info;
}
The idea is that AuthenticateAttribute instance would populate the UserInfo instance.
I want...
Hi everyone,
inheriting a class attribute from a super class and later changing the value for the subclass works fine:
class Unit(object):
value = 10
class Archer(Unit):
pass
print Unit.value
print Archer.value
Archer.value = 5
print Unit.value
print Archer.value
leads to the output:
10
10
10
5
which is just fine: Archer ...
Hi,
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[de...
Python noob here,
Currently I'm working with SQLAlchemy, and I have this:
from __init__ import Base
from sqlalchemy.schema import Column, ForeignKey
from sqlalchemy.types import Integer, String
from sqlalchemy.orm import relationship
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
username ...
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!
...
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 docume...
Hi,
I'm trying to write a ruby class that works similarly to rails activerecord model in the way that attributes are handled:
class Person
attr_accessor :name, :age
# init with Person.new(:name => 'John', :age => 30)
def initialize(attributes={})
attributes.each { |key, val| send("#{key}=", val) if respond_to?("#{key}=") }
...
I'd like to have some data descriptors as part of a class. Meaning that I'd like class attributes to actually be properties, whose access is handled by class methods.
It seems that Python doesn't directly support this, but that it can be implemented by subclassing the type class. So adding a property to a subclass of type will cause i...
In python, I want to have a class attribute which is a dictionary with initialized values.
I wrote the code like the below.
class MetaDataElement:
(MD_INVALID, MD_CATEGORY, MD_TAG) = range(3)
mapInitiator2Type = {'!':MetaDataElement.MD_CATEGORY,
'#':MetaDataElement.MD_TAG}
But when I try to run this c...
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 s...
I want to remove the security permissions from a class I don't have access to the source for. Is it possible to, via reflection, remove or modify the attribute?
[...PermissionSet(SecurityAction.InheritanceDemand, Name="FullTrust"), PermissionSet(SecurityAction.LinkDemand, Name="FullTrust")]
after some consideration, it's occurred to...
After wrestling with a bunch of uncaught exceptions when trying to serialize my classes and subclasses, I've finally understood what my problem had been: [Serializable] isn't inherited by subclasses when applied to a base class. I'm still really fuzzy about C# Attributes in general, but I do understand that when creating a custom Attribu...