Python Forum
Help using class methods
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help using class methods
#2
To use the class you have to create an instance so you code should have something like:

alphabet=Alphabet()

alphabet['k'] # will call alphabet.__getitem__('k')
Note that nothing says how you initialize the mapping with data, that's up to you. And __getitem__ can just as well compute the answer instead of
looking it up:

import collections
 
class Alphabet(collections.Mapping):
    
    def __len__(self):
       pass

    def __iter__(self):
       pass

    def __getitem__(self,key):
        return ord((key.lower()[0]))-96
   
alphabet=Alphabet()
print (alphabet['a'])
print (alphabet['A'])
print (alphabet['z'])
Btw, using numpy just to read a file is gross.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Messages In This Thread
Help using class methods - by shaynehansen - Jun-19-2017, 10:46 PM
RE: Help using class methods - by Ofnuts - Jun-21-2017, 10:00 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020