Is there a built-in method in Python to get an array of all a class' instance variables? For example, if I have this code:
class hi:
def __init__(self):
self.ii = "foo"
self.kk = "bar"
Is there a way for me to do this:
>>> mystery_method(hi)
["ii", "kk"]
Thanks guys!
Edit: I originally had asked for class variables erron...
I'm having trouble in getting the singleton pattern to initialize a instance variable in smalltalk. (here is a link to another implementation for clarification)
this is what I have:
new
^UniqueInstance ifNil: [UniqueInstance := self basicNew.
UniqueInstance: instanceVar := Object new. ].
that last line (UniqueInsta...
Hello,
can I initialize an instance variable in Java, when I initialise it when I declare it, and initialise it with the return value of a method, which I define later in the class. Something like this:
public class MyClass {
integers[] myArray = new integers[length()];
int length() {
....
}
}
length() gives me...
I'm really new to Ruby. And by new - less than 16 hours, but my boss gave me some Ruby code to add to. However, I found it was one giant file and not modular at all, so I decided to clean it up. Now that I've broken it up into several files/classes (generally speaking, 1 class per file,) I'm having problems piecing it together for it to ...
I'm probably doing something stupid but I can't figure out what it is.
The output I'm seeing from this program is
foo
test
What I'm expecting to see is
foo
abc
test
Does anyone see anything obviously wrong here?
class Foo
def initialize(l)
@label = l
end
def label
@label
end
def abc
@abc
en...
The function I'm looking at:
-(void)viewDidLoad {
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"statedictionary" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.statesZips = dictionary;
[dictionary release];
N...
I have some modules where I would like to use instance variables in. I'm currently initializing them like this :
module MyModule
def self.method_a(param)
@var ||= 0
# other logic goes here
end
end
I also could call a init method to initialize them :
def init
@var = 0
end
but this would mean I have to remember to alw...
Hello
My questions is in regards to this AR and its instance variable @saved
class PhoneNumber < ActiveRecord::Base
has_one :user
validates_presence_of :number
def self.create_phone_number( user, phone_hash )
@new_phone = PhoneNumber.new(phone_hash)
@user = user
PhoneNumber.transaction do
@user.phone_numbers << @new_phone
@new...
For me instance variables are simple data types like int or double. Everything that is created automatically when the object is created.
If an object creates additional objects - like everything that is it done with the NEW keyword - these are not instance variables.
Am I right or wrong?
...
I'm implementing an object that is almost identical to a set, but requires an extra instance variable, so I am subclassing the built-in set object. What is the best way to make sure that the value of this variable is copied when one of my objects is copied?
Using the old sets module, the following code worked perfectly:
import sets
cla...
I think I've been using these terms interchangably / wrongly!
...
Lets say I have this code:
<?php
class hello {
var $greeting = "hello";
function hello(){
echo $this->greeting;
return;
}
}
$hello1 = new hello;
$hello2 = new hello;
$hello4 = new hello;
?>
How do I get it to echo all the names of instantiated objects (and if possible their respective class), so that it ec...
class Hello
@hello = "hello"
def display
puts @hello
end
end
h = Hello.new
h.display
I created the class above. It doesn't print anything out. I thought the instance variable @hello was set during the class declaration. But when I call the display method the output is 'nil'. What's the correct way to do this?
...
<% form_tag(:action=>'update', :id=>@album.id) do %>
Title: <%= text_field(:album, :title) %><br>
Artist: <%= text_field(:album, :artist) %><br>
Genre: <%= text_field(:album, :genre) %><br>
Release Date: <%= datetime_select(:album, :release_date, :start_year=>1960) %><br>
<%= submit_tag("Update") %>
<% end %>
In the...
I can understand why you would need a class variable to keep track of things like the total number of objects that have been instantiated in that class.
And I can understand why you would need an instance variable to store attributes of a particular object in that class.
But class instance variables I just can't seem to justify.
As I ...
If I have a class with an attr_accessor, it defaults to creating an instance variable along with the corresponding getters and setters. But instead of creating an instance variable, is there a way to get it to create a class variable or a class instance variable instead?
...
Using local variables seems advisable in a partial that could be used application-wide to avoid dependencies across the application.
But within a single controller it seems acceptable to reference instance variables that you know will be available in all of the actions that use the partial.
If you do this, there seems to be a risk, h...
So I'm a bit rusty getting back into programming and I can't seem to find a good reference for understanding the structure for what I am trying to achieve. So, without further ado I am looking at creating and Class object such as.
#import Assets.h
@interface MainRecord: NSObject {
Assets* assets;
...
}
...
@end
Having a class...
I have a class instance variable on one of my AR classes. I set its value at boot with an initializer and, after that, never touch it again except to read from it. In development mode, this value disappears after the first request to the web server. However, when running tests, using the console or running the production server this d...
I want to make an instance variable that can't be accessed from outside. Is something like that possible in objective-c? I remember Apple has private variables and stuff like that, but if people know about them, they can use them. Apple calls that "private API", but obviously others can access that stuff if they find out what's in there....