-1

I an new to object oriented python coding. And recently working on them.

Suppose that i have a code like:

class Student(object):
    def __init__(self, name, age, job):
        self.name = name
        self.age = age
        self.job = job

if i pass instance to it like:

a = Student('Ram', 12, False)
b = Student('Hira', 34, True)

Then i can uses class attributes on a and b like:

>>>a.name
Ram
>>>b.age
34

is it possible that i can access instances like:

>>>z=Student()
>>>z.name
Ram
Hira
>>>z.age
12
34

Any idea ??

KHUSHAL SINGH
  • 33
  • 2
  • 8
  • I don't understand what you are trying to do at all. – timgeb Dec 19 '15 at 20:07
  • 1
    @timgeb He's trying to get the attributes of all instances that he created. – pushkin Dec 19 '15 at 20:09
  • 1
    There is no built-in way to do this. You would have to put the instances in a list and have a function `age`, for instance, that iterates through the list and prints (or returns a list) of the instance's age. Alternatively, you could use list comprehension (look at Klaus' answer) - arguably a better strategy. – pushkin Dec 19 '15 at 20:10
  • @pushkin aah, that makes sense – timgeb Dec 19 '15 at 20:10
  • @pushkin can you explain it by example. Thanks – KHUSHAL SINGH Dec 19 '15 at 20:13
  • I think what you are looking for is *inside-out objects* (Google it). The technique was used in Perl, but rarely (if ever) needed in Python. Basically you hold a dictionary for each attribute as a class variable. The key is the `id` of each object. Most Pythonistas would consider them horrible. Ask yourself why you need this feature. Maybe your design need to be reconsidered? – cdarke Dec 19 '15 at 20:14
  • @cdarke Django uses this kind of method in creating and accessing model. I am using Dajngo but somehow i have to create that kind of structure. – KHUSHAL SINGH Dec 19 '15 at 20:18
  • @KHUSHALSINGH Look at Klaus' answer. – pushkin Dec 19 '15 at 20:20
  • @pushkin got it. Thanks – KHUSHAL SINGH Dec 19 '15 at 20:22

2 Answers2

2

Not like that. But you can store the students in a list and then use list comprehensions on it:

students = []
students.append(Student('Ram', 12, False))
students.append(Student('Hira', 34, True))

ages = [s.age for s in students]

This will give you a list of all ages.

You can even add a condition:

adult_ages = [s.age for s in students if s.age >= 18]
Klaus D.
  • 13,874
  • 5
  • 41
  • 48
2

Yes you can! Define an arbitrary attribute list in the class and using weakref access all of the class's attributes.

weakref is used to create weak references to an object(a class instance, ideally) through proxy method. The magic line means that get all the attributes of this class and append it to the list(here, instances).

Here is the code:

import weakref as wr
class A:
    instances = []
    def __init__(self, name, age, job):
        self.__class__.instances.append(wr.proxy(self)) #magic
        self.name = name
        self.age = age
        self.job = job

a1 = A('mario', 23, 'student')
a2 = A('cocoo', 25, 'student')
a3 = A('romeo', 27, 'student')

for instance in A.instances:
    print(instance.name)

for instance in A.instances:
    print(instance.age)

Sample output:

mario
cocoo
romeo
23
25
27
kmario23
  • 57,311
  • 13
  • 161
  • 150