The inheritance of attributes using __init__

When writing the __init__ function for a class in python, you should always call the __init__ function of its superclass. We can use this to pass the relevant attributes directly to the superclass, so your code would look like this: class Person(object): def __init__(self, name, phone): self.name = name self.phone = phone class Teenager(Person): def … Read more

How can I return a default value for an attribute? [duplicate]

You should use the getattr wrapper instead of directly retrieving the value of id. a = getattr(myobject, ‘id’, None) This is like saying “I would like to retrieve the attribute id from the object myobject, but if there is no attribute id inside the object myobject, then return None instead.” But it does it efficiently. … Read more

Compelling Reasons to Use Marker Interfaces Instead of Attributes

I generally avoid “marker interfaces” because they don’t allow you to unmark a derived type. But that aside, here are some of the specific cases that I have seen where marker interfaces would be preferable to built-in meta-data support: Run-time performance sensitive situations. Compatibility with languages that don’t support annotation or attributes. Any context where … Read more

How do attribute classes work?

I haven’t use attributes in my day-to-day work before, but I have read about them. Also I have done some tests, to back up what I’ll say here. If I’m wrong in any place – feel free to tell me this 🙂 From what I know, attributes are not acting as regular classes. They aren’t … Read more

How to pass objects into an attribute constructor

The values into attributes are limited to simple types; for example, basic constants (including strings) and typeof… you can’t use new or other more complex code. In short; you can’t do this. You can give it the type though: [PropertyValidation(typeof(NullOrEmptyValidatorScheme)] i.e. the PropertyValidation ctor takes a Type, and use Activator.CreateInstance inside the code to create … Read more

Obsolete attribute causes property to be ignored by XmlSerialization

EDIT: After reading a MS Connect article, it appears that .Net 2.0 has a ‘feature’ where it makes ObsoleteAttribute equivalent to XmlIgnoreAttribute without any notification in the documentation. So I’m going to revise my answer to say that the only way to have your cake and eat it too in this instance is to follow … Read more

How to ignore compiler warning when using Obsolete attribute on a class used with a KnownType attribute

Use this to disable the corresponding warnings just before the offending line: #pragma warning disable 612, 618 And reenable the warnings after it: #pragma warning restore 612, 618 Curiously enough, there’re 2 warnings related to this: CS0612 and CS0618 – one is for [Obsolete] and the other for [Obsolete(“Message”)]. Go figure…

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