简体   繁体   中英

Unpacking instance variables by making container iterable

I just want to be able to unpack the instance variables of class foo, for example:

x = foo("name", "999", "24", "0.222")
a, b, c, d = *x
a, b, c, d = [*x]

I am not sure as to which is the correct method for doing so when implementing my own __iter__ method, however, the latter is the one that has worked with mixed "success". I say mixed because doing so with the presented code appears to alter the original instance object x , such that it is no longer valid.

class foo:
  def __init__(self, a, b, c, d):
    self.a = a
    self.b = b
    self.c = c
    self.d = d

  def __iter__(self):
    return iter([a, b, c, d])

I have read the myriad posts on this site regarding __iter__ , __next__ , generators etc., and also a python book and docs.python.org and seem unable to figure what I am not understanding. I've gathered that __iter__ needs to return an iterable (which can be just be self , but I am not sure how that works for what I want). I've also tried various ways of playing around with implementing __next__ and iterating over vars(foo).items() , either by casting to a list or as a dictionary, with no success.

I don't believe this is a duplicate post on account that the only similar questions I've seen present a single list sequence object attribute or employ a range of numbers instead of a four non-container variables.

If you want the instance's variables, you should access them with .self :

def __iter__(self):
    return iter([self.a, self.b, self.c, self.d])

with this change,

a, b, c, d = list(x)

will get you the variables.


You could go to the more risky method of using vars(x) or x.__dict__ , sort it by the variables name (and that's why it is also a limited one, the variables are saved in no-order), and extract the second element of each tuple. But I would say the iterator is definitely better.

You can store the arguments in an attribute ( self.e below) or return them on function call:

class foo:
  def __init__(self, *args):
    self.a, self.b, self.c, self.d = self.e = args

  def __call__(self):
    return self.e

x = foo("name", "999", "24", "0.222")
a, b, c, d = x.e
# or
a, b, c, d = x()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM