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 9024683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:05:08+00:00 2026-06-16T06:05:08+00:00

For example take a simple class representing a person. The only class attribute is

  • 0

For example take a simple class representing a person. The only class attribute is a string representing the persons name. I want to make sure that nobody tries to pass the constructor some other type of object like an int, or list…etc. This was my first attempt below I thought that this would return an obj of type None if the argument was not a str but it still seems to return a Person obj. I come from a “more” strongly typed language background and I am a little confused as how to handle my self in python. What does pythonic style say about this situation and type safety more generally? Should I raise an exception? Or find a way to return None? Or something else entirely.

class Person: 

    name = None

    def __init__(self, name):
        if not isinstance(name, str):
            return None
        self.name = name
  • 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-16T06:05:10+00:00Added an answer on June 16, 2026 at 6:05 am

    You’d want to raise an error within the __init__ method:

    if not isinstance(name,basestring):
        raise TypeError("I don't think that is a name ...")
    

    *Note that basestring also includes unicode for python2.x, but isn’t available in python3.x.

    Careful though, there is nothing here to prevent a user from re-setting the person’s name to a list after the person has been constructed.

    jack = Person("Jack")
    jack.name = ["cheese","steak"]  #???
    

    If you want to have this safety built in, you’ll need to start learning about property.

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

Sidebar

Related Questions

I often take the classes that linq2sql generates and create a simple data-only class
Take this simple example: class Program { static void Main(string[] args) { var windsorContainer
Take this sample class as an example: [AttributeUsage(AttributeTargets.All, AllowMultiple=true)] public class BugFixAttribute : System.Attribute
Take my domain class for example public class Person { private Integer id; private
Take the following simple object model for example: class Course has_many :enrollments has_many :students,
In that simple case: public class Person { public int Id {get;set;} public int
I want to make a simple program for my number theory class. We're learning
Take this very simple form for example: class SearchForm(Form): q = forms.CharField(label='search') This gets
To take a simple example String.isEmpty() was made available in API level 9. If
Take the following simple example: interface IVehicle { } class Car : IVehicle {

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.