Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError with global
#1
class titlescreen:
    global drawing 
    drawing ='...drawing...'

    def drawbackground(filename,constant=True):
        if constant==True:
            print ('===non-animated title screen===')
            print(drawing)
        elif constant==False:
            print('===animated title screen===')
            print (drawing)
        else:
            print('===unknown background type===')
            print('===cannot draw background===')



titlescreen.drawbackground('yuzsgxsw')
i need to make the variable drawing global before using it,if not it will give NameError,why?
the drawing variable is still titlescreen class ,right?
  
At the same time,can anyone teach me how to use the keyword self?
Reply
#2
(Feb-18-2017, 03:34 AM)hsunteik Wrote:
class titlescreen:
    global drawing 
    drawing ='...drawing...'

    def drawbackground(filename,constant=True):
        if constant==True:
            print ('===non-animated title screen===')
            print(drawing)
        elif constant==False:
            print('===animated title screen===')
            print (drawing)
        else:
            print('===unknown background type===')
            print('===cannot draw background===')



titlescreen.drawbackground('yuzsgxsw')
i need to make the variable drawing global before using it,if not it will give NameError,why?
the drawing variable is still titlescreen class ,right?
  
At the same time,can anyone teach me how to use the keyword self?

You are not using classes properly:
class TitleScreen: # using CamelCase for classes is a good habit
    # This defines a class variable 
    # Attributes are defined by being set in the constructor
    # (the __init__(self,...) function
    drawing ='...drawing...'
 
# Note the "self" argument in method arguments. 
# "self" is the instance on which the methods are called.
# It is automatically added and set by the method call mechanism in Python
# Inside the method you use "self" to reference class variables
# or instance variables (these latter are better called "attributes")
    def drawbackground(self,filename,constant=True):
        if constant==True:
            print ('===non-animated title screen===')
            print(self.drawing)
        elif constant==False:
            print('===animated title screen===')
            print (self.drawing)
        else:
            print('===unknown background type===')
            print('===cannot draw background===')
 
ts=TitleScreen() # Create class instance
ts.drawbackground('yuzsgxsw') # "self" will be add befire first paramater
ts.drawbackground('yuzsgxsw',False) # "self" will be add befire first paramater
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
A tutorial on class basics.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError for global variable leviporton 3 2,501 Jul-10-2020, 06:51 PM
Last Post: bowlofred
  NameError: NameError: global name 'BPLInstruction' is not defined colt 7 4,300 Oct-27-2019, 07:49 AM
Last Post: Larz60+
  Global variable does not seem to be global. Columbo 6 3,617 Jul-15-2019, 11:00 PM
Last Post: Columbo
  NameError: Global Name is not defined MartinBerlin 3 41,189 Aug-25-2018, 09:03 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020