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

  • Home
  • SEARCH
  • 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 8854163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:49:23+00:00 2026-06-14T13:49:23+00:00

Having a class class A(object): z = 0 def Func1(self): return self.z def Func2(self):

  • 0

Having a class

class A(object):
    z = 0

    def Func1(self):
        return self.z

    def Func2(self):
        return A.z

Both methods (Func1 and Func2) give the same result and are only included in this artificial example to illustrate the two possible methods of how to address z.

The result of Func* would only differ if an instance would shadow z with something like self.z = None.

What is the proper python way to access the class variable z using the syntax of Func1 or Func2?

  • 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-14T13:49:24+00:00Added an answer on June 14, 2026 at 1:49 pm

    I would say that the proper way to get access to the variable is simply:

    a_instance.z  #instance variable 'z'
    A.z           #class variable 'z'
    

    No need for Func1 and Func2 here.


    As a side note, if you must write Func2, it seems like a classmethod might be appropriate:

    @classmethod
    def Func2(cls):
        return cls.z
    

    As a final note, which version you use within methods (self.z vs. A.z vs. cls.z with classmethod) really depends on how you want your API to behave. Do you want the user to be able to shadow A.z by setting an instance attribute z? If so, then use self.z. If you don’t want that shadowing, you can use A.z. Does the method need self? If not, then it’s probably a classmethod, etc.

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

Sidebar

Related Questions

Having a snippet like this: import yaml class User(object): def __init__(self, name, surname): self.name=
Having a class like this: class Spam(object): def __init__(self, name=''): self.name = name eggs
I have an object roughly like so: class Hand(object): def __init__(self, finger_names, finger_lengths, nail_sizes):
I have the following class hierarchy: class A(object): def __init__(self, filename, x): self.x =
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
I started out with something like this: class Client (object): def __init__ (self): self.state
Say I have the following: class Parent(object): [...] def func(self, list): for item in
I'm having a problem understanding why a class object I created fails to return
Imagine I have a class A class A(object): def __init__(self, x, y) self.x =

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.