Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8663539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:00:39+00:00 2026-06-12T17:00:39+00:00

Whilest learning Python 3 and converting some of my code from Java to Python

  • 0

Whilest learning Python 3 and converting some of my code from Java to Python 3.3 I came across a small problem I haven’t been able to fix.
In Java I have this code (just dummy code to make it smaller):

public enum Mapping {
    C11{public int getMapping(){ return 1;}},
    C12{public int getMapping(){ return 2;}},
    public abstract int getMapping();
}

String s = "C11";
System.out.println(Mapping.valueOf(s))

Works fine and prints the requisted ‘1’
Trying to do this in Python doesn’t work that easy (yet). I tried to imitate an Enum with:

class Mapping:
    C11=1
    C12=2

s = 'C11'
print(Mapping.Mapping.(magic should happen here).s)

Unfortunately I have no idea how to convert a string to an attribute to be called like that (or something similar).
I need this because I have a HUGE list in the class Mapping and need to convert seemingly random words read from a text file to an integer mapping.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:00:40+00:00Added an answer on June 12, 2026 at 5:00 pm

    You are looking for getattr:

    >>> getattr(Mapping, s)
    1
    

    From the documentation:

    getattr(object, name[, default])

    Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whilst working on some generally horrible Javascript code this morning, I came across the
Whilst reading through the DirectWrite source code I came across the following struct: ///
I've encountered a problem whilst learning how to texture in OpenGL (from OpenGL 5th
Whilst reading jQuery Cookbook (Oreilly) last night I came across an each function that
Whilst I'd love to solve this problem in python, I'm stuck in Delphi for
I've grown accustomed to using IPython to try things out whilst learning Python, and
I am learning Python (I have a C/C++ background). I need to write something
I have been making a matrix class (as a learning exercise) and I have
I've been learning android development (in Eclipse) the past week. I'm following online courses
Whilst 'investigating' finalisation (read: trying stupid things) I stumbled across some unexpected behaviour (to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.