4

Sorry about the title, I'm not sure how to word this question in a more general sense. I'm working on a brainfuck parser. The language has 8 kinds of tokens, which I currently represent with the following classes:

# Types of lexical tokens
class Token: pass
class IncPtrToken(Token): pass
class DecPtrToken(Token): pass
class IncByteToken(Token): pass
class DecByteToken(Token): pass
class OutputByteToken(Token): pass
class InputByteToken(Token): pass
class LoopStartToken(Token): pass
class LoopEndToken(Token): pass

In another language I would probably use an enumeration to represent the different types, but this seems like a reasonable alternative in Python.

I have read that there are other ways of representing enums in Python, but I think this style is suitable because some of these entities may carry additional data with them. For example, when representing types of nodes in a parser, a node like a loop would contain a member with the nodes inside the loop.

Community
  • 1
  • 1
Overv
  • 8,433
  • 2
  • 40
  • 70
  • 2
    Yes. The same thing is done for the Exception class. http://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python – blakev Aug 27 '13 at 22:23
  • @user2357112 I know it's extreme overkill, but I kind of want to structure it like a classic compiler with the lexer, parser and code generation stages. – Overv Aug 27 '13 at 22:27
  • Python lacks real enumerations. What you are doing is perfectly normal. – Eric Urban Aug 27 '13 at 22:43
  • Python 3.4 has enums now. you can check the [PEP435](http://www.python.org/dev/peps/pep-0435) – Hassek Aug 27 '13 at 22:44
  • @Hassek True, but some of these may need to store data at some point. – Overv Aug 27 '13 at 22:46
  • My bad, didn't read the whole post, if you want to add additional data I would probably use Enums and a class with `type` (the enum) and a list of nodes for example, instead of using that many classes. – Hassek Aug 27 '13 at 22:49

2 Answers2

1

Python gains an official Enum class in 3.4.

If you are using Python between 2.4 and 3.3 check the enum34 backport.

It is possible to add other behavior and state to a custom Enum class as well.

Community
  • 1
  • 1
Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
  • Accepting because they are appropriate in this case. Is it acceptable to alternate between enums and classes in the same program for when one fits the requirements better? – Overv Aug 27 '13 at 22:52
  • Of course. Python is not a bondage language -- use whatever makes sense for each case. – Ethan Furman Aug 27 '13 at 22:53
1

Type :

import this

(also named PEP 20) explains shortly what is pythonic.

"If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea."

I don't know how you intend to do your brainfuck interpreter. I suppose you don't intend to put the specific functions (related to what the given ...Token does) inside the ...Token class.

I would use functions or, if they are empty, a list to do so, replaced by an enum as soon as Py 3.4 is released.

In a nutshell : hard to explain? Keep thinking!

Wjars
  • 99
  • 5