Questions tagged [object]

An object is any entity that can be manipulated by commands in a programming language. An object can be a value, a variable, a function, or a complex data-structure. In object-oriented programming, an object refers to an instance of a class.

Objects in object-oriented programming (OOP) are data structures combined with the associated processing routines. Classes are sets of objects, while objects are instances of sets. Objects have members and methods, defining their properties and their abilities. Classes can have their own members and methods, which define the properties and abilities of the set of objects. For instance, if we have a Bird class, its objects might have an age property and a fly ability, while the Bird class might have a number of birds or a migrate ability, which is applicable for the set. Class-level methods are called static, or shared. For instance, a file could be represented as an object: a collection of data and the associated read and write routines. In typical object-oriented languages, all objects are instances of classes.

Properties of an object

Three properties characterize objects:

  • Identity: the property of an object that distinguishes it from other objects
  • State: describes the data stored in the object
  • Behavior: describes the methods in the object's interface by which the object can be used

See also:

  • (Used as a template for creating new objects)
  • (Object-oriented programming)

Resource

64898 questions
788
votes
16 answers

How do I call a parent class's method from a child class in Python?

When creating a simple object hierarchy in Python, I'd like to be able to invoke methods of the parent class from a derived class. In Perl and Java, there is a keyword for this (super). In Perl, I might do this: package Foo; sub frotz { …
jjohn
  • 9,708
  • 4
  • 20
  • 21
785
votes
12 answers

How to print instances of a class using print()?

When I try to print an instance of a class, I get an output like this: >>> class Test(): ... def __init__(self): ... self.a = 'foo' ... >>> print(Test()) <__main__.Test object at 0x7fc9a9e36d60> How can I make it so that the print will…
Ashwin Nanjappa
  • 76,204
  • 83
  • 211
  • 292
768
votes
36 answers

How to get a subset of a javascript object's properties

Say I have an object: elmo = { color: 'red', annoying: true, height: 'unknown', meta: { one: '1', two: '2'} }; I want to make a new object with a subset of its properties. // pseudo code subset = elmo.slice('color', 'height') //=> {…
Christian Schlensker
  • 21,708
  • 19
  • 73
  • 121
766
votes
17 answers

What is the difference between __init__ and __call__?

I want to know the difference between __init__ and __call__ methods. For example: class test: def __init__(self): self.a = 10 def __call__(self): b = 20
sam
  • 18,509
  • 24
  • 83
  • 116
742
votes
13 answers

Difference between object and class in Scala

I'm just going over some Scala tutorials on the Internet and have noticed in some examples an object is declared at the start of the example. What is the difference between class and object in Scala?
Steve
  • 21,163
  • 21
  • 69
  • 92
708
votes
31 answers

How to get a key in a JavaScript object by its value?

I have a quite simple JavaScript object, which I use as an associative array. Is there a simple function allowing me to get the key for a value, or do I have to iterate the object and find it out manually?
arik
  • 28,170
  • 36
  • 100
  • 156
685
votes
10 answers

How to build a basic iterator?

How can I create an iterator in Python? For example, suppose I have a class whose instances logically "contain" some values: class Example: def __init__(self, values): self.values = values I want to be able to write code like: e =…
akdom
  • 32,264
  • 27
  • 73
  • 79
671
votes
11 answers

How to iterate (keys, values) in JavaScript?

I have a dictionary that has the format of dictionary = {0: {object}, 1:{object}, 2:{object}} How can I iterate through this dictionary by doing something like for ((key, value) in dictionary) { //Do stuff where key would be 0 and value would…
nbroeking
  • 8,360
  • 5
  • 20
  • 40
666
votes
21 answers

access key and value of object using *ngFor

I am a bit confused about how to get the key and value of an object in angular2 while using *ngFor for iterating over the object. I know in angular 1.x there is a syntax like ng-repeat="(key, value) in demo" but I don't know how to do the same in…
Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
661
votes
45 answers

How to convert a nested Python dict to object?

I'm searching for an elegant way to get data using attribute access on a dict with some nested dicts and lists (i.e. javascript-style object syntax). For example: >>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]} Should be accessible in…
Marc
  • 6,741
  • 3
  • 19
  • 8
657
votes
23 answers

Sort array of objects by one property

How can I sort this array of objects by one of its fields, like name or count? Array ( [0] => stdClass Object ( [ID] => 1 [name] => Mary Jane [count] => 420 ) [1] => stdClass Object …
Alex
  • 66,732
  • 177
  • 439
  • 641
646
votes
14 answers

Javascript / Chrome - How to copy an object from the webkit inspector as code

I am doing a console.log statement in my javascript in order to log a javascript object. I'm wondering if there's a way, once that's done - to copy that object as javascript code. What I'm trying to do is convert an object that was created using…
mheavers
  • 29,530
  • 58
  • 194
  • 315
637
votes
10 answers

Most efficient way to convert an HTMLCollection to an Array

Is there a more efficient way to convert an HTMLCollection to an Array, other than iterating through the contents of said collection and manually pushing each item into an array?
Tom
  • 15,527
  • 5
  • 48
  • 62
629
votes
16 answers

Can we instantiate an abstract class?

During one of my interview, I was asked "If we can instantiate an abstract class?" My reply was "No. we can't". But, interviewer told me "Wrong, we can." I argued a bit on this. Then he told me to try this myself at home. abstract class my { …
Ravi
  • 30,829
  • 42
  • 119
  • 173
625
votes
19 answers

How to iterate over a JavaScript object?

I have an object in JavaScript: { abc: '...', bca: '...', zzz: '...', xxx: '...', ccc: '...', // ... } I want to use a for loop to get its properties. And I want to iterate it in parts (not all object properties at…
nkuhta
  • 10,388
  • 12
  • 41
  • 54