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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:09:04+00:00 2026-06-05T22:09:04+00:00

Pep8 advises to always use cls as the first argument of a class method

  • 0

Pep8 advises to always use cls as the first argument of a class method definition.
Now suppose I want to use a class variable(in this case: cls.cartridge_state) that can also be used in an instance method (in this case: __init__). So for that I need to make the variable global(see code below). But instantiating FountainPen generates the following runtime error:

self.cartridge_state = cls.cartridge_state
NameError: global name 'cls' is not defined

But then again when I change global cartridge_state into global cls.cartridge_state I get a syntaxError when i try to import the module.

class FountainPen(object):
    cartridge_ink = "water-based"
    @classmethod
    def toggle_default_cartridge_state(cls):
        i = 0
        cartridge_states = ['non-empty','empty']
        global cartridge_state
        cls.cartridge_state = cartridge_states[i]
        i += 1

    def __init__(self):
        self.cartridge_state = cls.cartridge_state
        global number_of_refills
        self.number_of_refills = 0

    def write(self):
        print Pen.write(self)
        self.cartridge_state = "empty"
        return self.cartridge_state

    def refill(self):
        self.cartridge_state = "non-empty"
        self.number_of_refills += 1

How can I let the class variable cartridge_state be pep8 compliant and make this code work without errors?

  • 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-05T22:09:07+00:00Added an answer on June 5, 2026 at 10:09 pm

    A class attribute can be read via self:

    class FountainPen(object):
        cartridge_ink = "water-based"
        default_cartridge_state = "empty"
    
        @classmethod
        def toggle_default_cartridge_state(cls):
            if cls.default_cartridge_state == "empty":
                cls.default_cartridge_state = "non-empty"  
            else:
                cls.default_cartridge_state = "empty"
    
        def __init__(self):
            self.cartridge_state = self.default_cartridge_state
    
        def write(self):
            print Pen.write(self)
            self.cartridge_state = "empty"
            return self.cartridge_state
    
        def refill(self):
            self.cartridge_state = "non-empty"
            self.number_of_refills += 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use pep8 as my makeprg in order to check and fix
First, I am fully aware about PEP8 , but sometimes it isn't very clear
For checking code in python mode I use flymake with pyflakes Also I want
Let's assume OSCClient is a class and I want instances of a class to
First, I couldn't find an answer to this in PEP 8 . That doesn't
Given this Python class, implementing a Django form, how would you properly break this
This is somehow related to question about big strings and PEP8 . How can
I want my code to be PEP-8 compliant. However, reading the PEP8-page everytime I
Background: I'm using the (fantastic) Vim plugin python-mode , which includes the pep8 linter.
I want to know if there is any way to fix all the PEP-8

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.