correct way to define class variables in Python [duplicate]

This question already has answers here: difference between variables inside and outside of __init__() (12 answers) Closed 1 year ago. I noticed that in Python, people initialize their class attributes in two different ways. The first way is like this: class MyClass: __element1 = 123 __element2 = “this is Africa” def __init__(self): #pass or something … Read more

List view getListItemXmlAttributes method fails with child publication items

I have created a JS class to populate SG/Folder list view data, when items are modified. (As per Jaime’s approach) Everything works great when I operate on items in the publication they’re created in. Ex: I open a component or page and the custom locked by column immediately updates and shows my user name. However, … Read more

Accessing dict keys like an attribute?

I find it more convenient to access dict keys as obj.foo instead of obj[‘foo’], so I wrote this snippet: class AttributeDict(dict): def __getattr__(self, attr): return self[attr] def __setattr__(self, attr, value): self[attr] = value However, I assume that there must be some reason that Python doesn’t provide this functionality out of the box. What would be … Read more

How can I create an object and add attributes to it?

I want to create a dynamic object (inside another object) in Python and then add attributes to it. I tried: obj = someobject obj.a = object() setattr(obj.a, ‘somefield’, ‘somevalue’) but this didn’t work. Any ideas? edit: I am setting the attributes from a for loop which loops through a list of values, e.g. params = … Read more

Specify multiple attribute selectors in CSS

What is the syntax for doing something like: input[name=”Sex” AND value=”M”] Basically, I want to select the input element that has the attribute name=”Sex” as well as the attribute value=”M”: <input type=”radio” name=”Sex” value=”M” /> Elements such as the following should not be selected: <input type=”radio” name=”Sex” value=”F” /> 5 Answers 5

Python dictionary from an object’s fields

Do you know if there is a built-in function to build a dictionary from an arbitrary object? I’d like to do something like this: >>> class Foo: … bar=”hello” … baz = ‘world’ … >>> f = Foo() >>> props(f) { ‘bar’ : ‘hello’, ‘baz’ : ‘world’ } NOTE: It should not include methods. Only … Read more

Why use Ruby’s attr_accessor, attr_reader and attr_writer?

Ruby has this handy and convenient way to share instance variables by using keys like attr_accessor :var attr_reader :var attr_writer :var Why would I choose attr_reader or attr_writer if I could simply use attr_accessor? Is there something like performance (which I doubt)? I guess there is a reason, otherwise they wouldn’t have made such keys. … Read more