How to check if an object has an attribute in Python?

Try hasattr(): if hasattr(a, ‘property’): a.property See zweiterlinde’s answer below, who offers good advice about asking forgiveness! A very pythonic approach! The general practice in python is that, if the property is likely to be there most of the time, simply call it and either let the exception propagate, or trap it with a try/except … Read more

Can attributes be added dynamically in C#?

This really depends on what exactly you’re trying to accomplish. The System.ComponentModel.TypeDescriptor stuff can be used to add attributes to types, properties and object instances, and it has the limitation that you have to use it to retrieve those properties as well. If you’re writing the code that consumes those attributes, and you can live … Read more

What is the difference between class and instance attributes?

There is a significant semantic difference (beyond performance considerations): when the attribute is defined on the instance (which is what we usually do), there can be multiple objects referred to. Each gets a totally separate version of that attribute. when the attribute is defined on the class, there is only one underlying object referred to, … Read more

Get list of data-* attributes using javascript / jQuery

Actually, if you’re working with jQuery, as of version 1.4.3 1.4.4 (because of the bug as mentioned in the comments below), data-* attributes are supported through .data(): As of jQuery 1.4.3 HTML 5 data- attributes will be automatically pulled in to jQuery’s data object. Note that strings are left intact while JavaScript values are converted … Read more

How to Compare Flags in C#?

In .NET 4 there is a new method Enum.HasFlag. This allows you to write: if ( testItem.HasFlag( FlagTest.Flag1 ) ) { // Do Stuff } which is much more readable, IMO. The .NET source indicates that this performs the same logic as the accepted answer: public Boolean HasFlag(Enum flag) { if (!this.GetType().IsEquivalentTo(flag.GetType())) { throw new … Read more

What are the best practices for using Assembly Attributes?

We’re using a global file called GlobalAssemblyInfo.cs and a local one called AssemblyInfo.cs. The global file contains the following attributes: [assembly: AssemblyProduct(“Your Product Name”)] [assembly: AssemblyCompany(“Your Company”)] [assembly: AssemblyCopyright(“Copyright © 2008 …”)] [assembly: AssemblyTrademark(“Your Trademark – if applicable”)] #if DEBUG [assembly: AssemblyConfiguration(“Debug”)] #else [assembly: AssemblyConfiguration(“Release”)] #endif [assembly: AssemblyVersion(“This is set by build process”)] [assembly: AssemblyFileVersion(“This … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)