How to deal with Pylint’s “too-many-instance-attributes” message?

A linter’s job is to make you aware of potential issues with your code, and as you say in your question, it should not have the last word. If you’ve considered what pylint has to say and decided that for this class, the attributes you have are appropriate (which seems reasonable to me), you can … Read more

Are ints always initialized to 0?

Yes, class instance variables are always initialized to 0 (or nil, NULL, or false, depending on the exact data type). See the Objective-C 2.0 Programming Language: The alloc method dynamically allocates memory for the new object’s instance variables and initializes them all to 0—all, that is, except the isa variable that connects the new instance … Read more

Directly accessing an instance variable vs. Using an accessor method

self.attribute calls the method attribute. self.attribute = value calls the method attribute= with the argument value. @attribute and @attribute = value get/set the value of the instance variable @attribute. So basically they’re two entirely different things. However if you call attr_accessor :attribute it defines the method attribute to return @attribute and the method attribute=(value) to … Read more

What is the difference between ivars and properties in Objective-C

Number 1 differs from the other two by forward declaring the MyOtherObject class to minimize the amount of code seen by the compiler and linker and also potentially avoid circular references. If you do it this way remember to put the #import into the .m file. By declaring an @property, (and matching @synthesize in the … Read more

How can I initialize a module’s instance variables in Ruby?

Initialize them in the module definition. module MyModule # self here is MyModule @species = “frog” @color = “red polka-dotted” @log = [] def self.log(msg) # self here is still MyModule, so the instance variables are still available @log << msg end def self.show_log puts @log.map { |m| “A #@color #@species says #{m.inspect}” } end … Read more

Ruby Rspec : Testing instance variables without adding an accessor to source

You can use Object#instance_variable_get method to get value of any instance variable of the object like that: class Foo def initialize @foo = 5 # no accessor for that variable end end foo = Foo.new puts foo.instance_variable_get(:@foo) #=> 5 And Object#instance_variable_set can be used to set instance variable values: foo.instance_variable_set(:@foo, 12) puts foo.instance_variable_get(:@foo) #=> 12

How to pass a default argument value of an instance member to a method?

You can’t really define this as the default value, since the default value is evaluated when the method is defined which is before any instances exist. The usual pattern is to do something like this instead: class C: def __init__(self, format): self.format = format def process(self, formatting=None): if formatting is None: formatting = self.format print(formatting) … Read more

Access instance variable from outside the class

Yes, you can use instance_variable_get like this: class Hello def method1 @hello = “pavan” end end h = Hello.new p h.instance_variable_get(:@hello) #nil p h.method1 #”pavan” – initialization of @hello p h.instance_variable_get(:@hello) #”pavan” If the variable is undefined (first call of instance_variable_get in my example) you get nil. As Andrew mention in his comment: You should … Read more

Ruby Metaprogramming: dynamic instance variable names

The method you are looking for is instance_variable_set. So: hash.each { |name, value| instance_variable_set(name, value) } Or, more briefly, hash.each &method(:instance_variable_set) If your instance variable names are missing the “@” (as they are in the OP’s example), you’ll need to add them, so it would be more like: hash.each { |name, value| instance_variable_set(“@#{name}”, value) }