[Tutor] TypeError: 'str' object is not callable

Alan Gauld alan.gauld at yahoo.co.uk
Sat May 15 19:55:20 EDT 2021


On 15/05/2021 23:16, Mats Wichmann wrote:

> Or, since it's Python, which doesn't _force_ you to use getters and 
> setters, just dispense with the method entirely and access the path 
> attribute directly. So this should work:
> 
> import glob
> 
> class foo:
>      def __init__(self, path):
>          self.path = path
> 
> def main():
>      hs = []
>      drives = glob.glob('/dev/sd?')
>      for d in drives:
>          print(d)
>          hs.append(foo(d))
>      for e in hs:
>          print(e.path)

Even better dispense with the class entirely and
just use a variable:

def main():
      hs = []
      drives = glob.glob('/dev/sd?')
      for d in drives:
          print(d)
          hs.append(d)
      for e in hs:
          print(e)


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list