0

I have a python3 class defined as such:

class Institution(object):
    def __init__(self, *args):
        self.name    = args[0].strip()
        self.authors = set()
        [...]

As name describes the institution, I'd like name not to be an attribute of the institution but rather the base of the class.

Therefore, I changed the definition to:

class Institution(str,object):

and I'd like to be able to access self seen for a str point of view.

Within __init__(self, *args), I tried :

self = args[0].strip()                     # → 'str' object has no attribute 'authors'
str(self) = args[0].strip()                # → can't assign to function call
str.__init__(self, args[0].strip())        # → object.__init__() takes no parameters
super(str, self).__init__(args[0].strip()) # → object.__init__() takes no parameters
super(str, self) = args[0].strip()         # → can't assign to function call

Is there any way to achieve what I'm trying to do ?

Amxx
  • 3,020
  • 2
  • 24
  • 45
  • 1
    `str` itself inherits from `object`, no need to mix in both. `str` is an *immutable* type, so you'd need to override `__new__` instead. See the duplicate. – Martijn Pieters Aug 10 '16 at 12:27
  • 1
    Why would an institute having a name be a reason to inherit from `str`? Can't you just return that name from `__str__` or `__repr__`? – Pieter Witvoet Aug 10 '16 at 12:32
  • `__new__` solves what I hoped to achieve. Also, thank for pointing out `__str__` and `__repr__` which I didn't know (python sure isn't my main language) and which sound more appropriate here. – Amxx Aug 10 '16 at 12:35

0 Answers0