Questions tagged [instance-variables]

In object-oriented programming with classes, an instance variable is a variable defined in a class (i.e. a member variable), for which each object of the class has a separate copy.

In object-oriented programming with classes, an instance variable is a variable defined in a class (i.e. a member variable), for which each object of the class has a separate copy.

1506 questions
35
votes
4 answers

class variables is shared across all instances in python?

I started coding in python a week ago, it is my mistake i started coding using oops,classes and objects that soon. I assumed my C++ proficiency will help.... I got bit by the following code class A: var=0 list=[] def __init__(self): …
howtechstuffworks
  • 1,824
  • 4
  • 29
  • 46
34
votes
4 answers

Setting default/empty attributes for user classes in __init__

I have a decent level of programming, and get much value from the community here. However I have never had much academic teaching in programming nor worked next to really experienced programmers. Consequently I sometimes struggle with 'best…
Andy
  • 919
  • 2
  • 9
  • 22
34
votes
4 answers

Type Hints Convention for Instance Variables Python

I'm unsure of the Python convention for type hinting instance variables - I've been doing them within the __init__ constructor arguments like seen here: class LoggedVar(Generic[T]): def __init__(self, value: T, name: str, logger: Logger) ->…
hhprogram
  • 2,809
  • 3
  • 13
  • 25
34
votes
2 answers

Should I declare variables in interface or using property in objective-c arc?

approach 1: @interface MyController : UIViewController { UILabel *myText; } @property (nonatomic, strong) UILabel *myText; approach 2: @interface MyController : UIViewController @property (nonatomic, strong) UILabel *myText; approach…
fmchan
  • 760
  • 1
  • 11
  • 29
33
votes
7 answers

How do I set an attr_accessor for a dynamic instance variable?

I dynamically created an instance variable within my class: class Mine attr_accessor :some_var def intialize @some_var = true end def my_number num self.instance_variable_set "@my_#{num}", num end end How do I make @my_#{num}…
eywu
  • 2,654
  • 1
  • 22
  • 24
32
votes
4 answers

python: What happens when class attribute, instance attribute, and method all have the same name?

How does python differentiate a class attribute, instance attribute, and method when the names are the same? class Exam(object): test = "class var" def __init__(self, n): self.test = n def test(self): print "method :…
naren
  • 14,611
  • 5
  • 38
  • 45
31
votes
3 answers

Why don't instance fields need to be final or effectively final to be used in lambda expressions?

I'm practicing lambda expressions in Java. I know local variables need to be final or effectively final according to the Oracle documentation for Java SE 16 Lambda Body : Any local variable, formal parameter, or exception parameter used but not…
DamianGDO
  • 449
  • 4
  • 8
31
votes
7 answers

Instance variables in methods outside the constructor (Python) -- why and how?

My questions concern instance variables that are initialized in methods outside the class constructor. This is for Python. I'll first state what I understand: Classes may define a constructor, and it may also define other methods. Instance…
xax
  • 2,043
  • 4
  • 24
  • 28
30
votes
3 answers

How to create a variable name from the value of a string in Ruby on Rails?

I want to create an instance variable in a controller to be used in the view: foo = "bar" instance_variable_set("#{foo}", "cornholio") In the view, use @bar so that: @bar => "cornholio" This generates an error: 'bar' is not allowed as an instance…
B Seven
  • 44,484
  • 66
  • 240
  • 385
29
votes
2 answers

How can Ruby's attr_accessor produce class variables or class instance variables instead of instance variables?

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…
28
votes
4 answers

Why do instance variables seemingly disappear when inside a block?

Forgive me, guys. I am at best a novice when it comes to Ruby. I'm just curious to know the explanation for what seems like pretty odd behavior to me. I'm using the Savon library to interact with a SOAP service in my Ruby app. What I noticed is that…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
28
votes
7 answers

Object-Oriented Perl constructor syntax and named parameters

I'm a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I don't get what this line does at all, but I always…
Thomas Owens
  • 114,398
  • 98
  • 311
  • 431
27
votes
9 answers

Can non-static methods modify static variables

I am wondering how a non static method can modify a static variable. I know that static methods can only access other static methods and static variables. However, is the other side true? Can non-static methods access only non-static variables? For…
Brian
  • 7,098
  • 15
  • 56
  • 73
26
votes
9 answers

Why are instance variables in Java always private?

I'm newbie to Java and I'm learning about encapsulation and saw an example where instance variables are declared as private in a class. http://www.tutorialspoint.com/java/java_encapsulation.htm I have 2 queries: Why are instance variables…
Deepak
  • 2,094
  • 8
  • 35
  • 48
26
votes
5 answers

Ruby on Rails- :symbols, @iVars and "strings" - oh my!

New to Rails and trying to get my head around when/why to use :symbols, @ivars , "strings" within the framework. I think I understand the differences between them conceptually only one :symbol instance per project one @ivar per instance multiple…
Meltemi
  • 37,979
  • 50
  • 195
  • 293