Questions tagged [instance]

In object-oriented programming an instance is an occurrence or a copy of an object, whether currently executing or not.

Instances of a class share the same set of attributes, yet will typically differ in what those attributes contain. For example, a class "Employee" would describe the attributes common to all instances of the Employee class. For the purposes of the task being solved Employee objects may be generally alike, but vary in such attributes as "name" and "salary".

Related tags:

5388 questions
26
votes
2 answers

Django: Initializing a FormSet of custom forms with instances

Given the following models: class Graph(models.Model): owner = models.ForeignKey(User) def __unicode__(self): return u'%d' % self.id class Point(models.Model): graph = models.ForeignKey(Graph) date =…
knipknap
  • 5,934
  • 7
  • 39
  • 43
26
votes
3 answers

How to get instance given a method of the instance?

class MyClass: def myMethod(self): pass myInstance = MyClass() methodReference = myInstance.myMethod Now can you get a reference to myInstance if you now only have access to methodReference?
user975135
26
votes
5 answers

Create an anonymous class instance in python

Sometimes I need to create an anonymous class instance in python, just like c#: var o = new {attr1="something", attr2=344}; but in python I do it in this way: class Dummy: pass o = Dummy() o.attr1 = 'something' o.attr2 = 344 # EDIT 1 print o.attr1,…
pylover
  • 7,670
  • 8
  • 51
  • 73
26
votes
5 answers

Create instance of class in another class (with generic example)

I'm learning python via book and internet and I'm stuck on a class issue. 2 questions: How do I create an instance of one class in another (separate) class? How do I pass variables between the class and the nested(?) class? When I try to create an…
DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
26
votes
9 answers

Java Static vs Instance

So my coder friend hates using the static coding. Yet my Java program is full of it to link between classes, and I have a lot of them! Is it worth rewriting the whole code to remove the static method? Is there any advantage of using one over the…
Kezz
  • 1,680
  • 3
  • 19
  • 37
25
votes
2 answers

Why can't I call hash() on an apparently hashable method of an unhashable instance?

Let's say I have a dictionary: >>> d = {} It has a method clear(): >>> d.clear ... which has a __hash__ attribute: >>> d.clear.__hash__
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
25
votes
1 answer

Local Declaration "x" hides instance variable xcode warning

I've been have trouble understand this problem. If I change the variable name fifthViewController the error goes away but the view controller doesn't load. Lost. Once again it's probably something simple. Thanks in advance. Here is the…
Michael Robinson
  • 1,439
  • 2
  • 16
  • 23
25
votes
2 answers

SQLAlchemy: Modification of detached object

I want to duplicate a model instance (row) in SQLAlchemy using the orm. My first thought was to do this: i = session.query(Model) session.expunge(i) old_id = i.id i.id = None session.add(i) session.flush() print i.id #New ID However, apparently…
EB.
  • 3,383
  • 5
  • 31
  • 43
24
votes
2 answers

Updating Class variable within a instance method

class MyClass: var1 = 1 def update(value): MyClass.var1 += value def __init__(self,value): self.value = value MyClass.update(value) a = MyClass(1) I'm trying to update a class variable(var1) within a…
f.rodrigues
  • 3,499
  • 6
  • 26
  • 62
24
votes
3 answers

Java Homework Help (Accessing Static Member via Instance Reference)

Here is my homework question: Write a class declaration for a class “Clock”. It should have instance variables for hours, minutes, seconds (all integers). It should also have a toString() method to show the time in the format shown below. Write a…
Bit Deception
  • 297
  • 1
  • 3
  • 7
24
votes
6 answers

Instance methods in modules

Consider the following code: module ModName def aux puts 'aux' end end If we replace module with class, we can do the following: ModName.new.aux Modules cannot be instanced, though. Is there a way to call the aux method on the module?
freenight
  • 1,069
  • 3
  • 13
  • 19
24
votes
10 answers

Getting an instance name inside class __init__()

While building a new class object in python, I want to be able to create a default value based on the instance name of the class without passing in an extra argument. How can I accomplish this? Here's the basic pseudo-code I'm trying for: class…
Akoi Meexx
  • 767
  • 2
  • 6
  • 15
24
votes
8 answers

JavaScript: How to create a new instance of a class without using the new keyword?

I think the following code will make the question clear. // My class var Class = function() { console.log("Constructor"); }; Class.prototype = { method: function() { console.log("Method");} } // Creating an instance with new var object1 = new…
avernet
  • 30,895
  • 44
  • 126
  • 163
24
votes
4 answers

Should I make this XmlSerializer static?

I've got a class which uses an XmlSerializer in its Read/WriteXml methods. The Serializer is currently private readonly. public class Foo : IXmlSerializable { private Bar _bar = new Bar(); private readonly XmlSerializer serBar = new…
mafu
  • 31,798
  • 42
  • 154
  • 247
23
votes
2 answers

Static Variable Instances and AppDomains, what is happening?

I have public static class A { public static string ConnString; } [Serializable] public class Test{ // Accesing A's field; public string ConnString{get{return A.ConnString;}set{A.ConnString=value;}} } void Main() { A.ConnString =…
Thanasis Ioannidis
  • 2,981
  • 1
  • 30
  • 50