2017-08-26 4 views
1

コード内にglobal userという配置を使用して私はかなり敬遠しましたが、どこかに見つからなかったかどうかを確認するためには表示されません。基本的に私はここに見られるこのコードの塊でグローバルuser変数を割り当てた後userInstance.getName()のために呼び出したとき:グローバル変数は関数によって編集されていません

if(userName in nameList): 
    for userdata in pklList: 
     if userdata.getName() == userName: 
      global user 
      user = userdata 
      print("user data found for user: " + user.getName()) 

が、実際にグローバル変数にそれを作るためには表示されません。ここでのコードのフルバージョンは、現時点では次のとおりです。

import praw 
import time 
import re 
import pickle 
from classes import User 



USERAGENT = 'web:CredibilityBot:v0.1 (by /u/ThePeskyWabbit)' 
FOOTER = "^^I ^^am ^^a ^^bot! ^^I ^^am ^^currently ^^in ^^test ^^phase. ^^Read ^^about ^^me ^^[here](https://pastebin.com/jb4kBTcS)." 
PATH = "C:\\Users\\JoshLaptop\\PycharmProjects\\practice\\commented.txt" 
user = User.User("ERROR") 

commentFile = open(PATH, 'rb') 
commentList = commentFile.read().splitlines() 
commentFile.close() 

pkl = open("userpkl.pkl", 'rb') 
pklList = [] 
print(pklList) 

try: 
    pickle.load(pkl) 
    while(True): 
     pklList.append(pickle.load(pkl)) 
except EOFError: 
    pass 
pkl.close() 

nameList = [] 
try: 
    for data in pklList: 
     nameList.append(str(data.getName())) 
except: 
    pass 

print(pklList) 
print(nameList) 


def addPoint(comment): 
    message = "current name for user: " + user.getName() 
    #userInstance.addScore() 
    #userInstance.addComment(comment) 
    #message = "Bullshit noted! " + userInstance.getName() + " now has a Bullshit rating of " + userInstance.getScore() + "\n\n" + FOOTER 
    return message 

def getRating(): 
    message = user.getName() + " has a Bullshit rating of: " + user.getScore() 
    return message 

def getCommentList(): 
    bullshitComments = user.getComments() 
    return bullshitComments 



auth = True 
def authenticate(): 
    print("Authenticating...") 
    reddit = praw.Reddit('bot1', user_agent=USERAGENT) 
    print("Authenticated as {}\n" .format(reddit.user.me())) 
    return reddit 

commentLink = None 

actions = {"!bullshit": addPoint(commentLink), "!bullshitrating": getRating(user), "!bullshitdetail":getCommentList(user)} 
stringList = ["!bullshit", "!bullshitrating", "!bullshitdetail"] 

while(auth): 
    try: 
     reddit = authenticate() 
     auth = False 
    except: 
     print("Authentication Failed, retying in 30 seconds.") 
     time.sleep(30) 


def runBot(): 
    SUBREDDITS = 'test' 
    global user 

    while(True): 
     print("Scraping 1000 comments...") 
     for comment in reddit.subreddit(SUBREDDITS).comments(limit=1000): 

      for word in stringList: 
       match = re.findall(word, comment.body.lower()) 

       if match: 
        id = comment.id 
        commentFile = open(PATH, 'r') 
        commentList = commentFile.read().splitlines() 
        commentFile.close() 

        if(id not in commentList): 

         print(match[0] + " found in comment: " + "www.reddit.com" + str(comment.permalink())) 
         commentLink = "www.reddt.com" + str(comment.parent().permalink()) 
         print("Bullshit comment is: " + commentLink) 

         print("searching for user data") 
         userName = str(comment.parent().author) 
         flag = True 

         if(userName in nameList): 
          for userdata in pklList: 
           if userdata.getName() == userName: 
            user = userdata 
            print("user data found for user: " + user.getName()) 


         elif comment.parent().author is not None: 
          print("no user found, creating user " + userName) 
          user = User.User(userName) 
          f = open("userpkl.pkl", 'ab') 
          pickle.dump(user, f) 
          f.close() 
          nameList.append(userName) 
          print("added to user to pkl file") 

         else: 
          print("username could not be retrieved.") 
          print("adding ID to log\n") 
          commentFile = open(PATH, 'a') 
          commentFile.write(id + "\n") 
          commentFile.close() 
          flag = False 

         if(flag): 
          try: 
           print(actions[match[0]]) 
           #print("sending reply...") 
           #comment.reply(actions[match[0]]) 
           #print("Reply successful. Adding comment ID to log\n") 
           #commentFile = open(PATH, 'a') 
           #commentFile.write(id + "\n") 
           #commentFile.close() 

          except: 
           print("Comment reply failed!\n") 




runBot() 

といただきました!奇妙な私は、コードのafformentioned snippit内user.getName()を呼び出すときに、私が呼ぶとき、それはないとして、それが正しい名前ではなく「エラー」が出力されますそれは私のaddPoint()機能です。

print文の出力は次のようにPythonで

C:\Python36-32\python.exe C:/Users/JoshLaptop/PycharmProjects/practice/TestBot.py 
[] 
[<classes.User.User object at 0x03B59830>, <classes.User.User object at 0x03816430>] 
['PyschoWolf', 'ThePeskyWabbit'] 
Authenticating... 
Authenticated as CredibilityBot 

Scraping 1000 comments... 
!bullshit found in comment: link deleted for privacy 
Bullshit comment is: link deleted for privacy 
searching for user data 
user data found for user: PyschoWolf 
current name for user: ERROR 
!bullshit found in comment: link deleted for privacy 
Bullshit comment is: link deleted for privacy 
searching for user data 
user data found for user: ThePeskyWabbit 
current name for user: ERROR 
+0

"エラー"はどこから来ていますか?私はあなたが "エラー"を出力するコードのどこにも見ることができません。エラーメッセージが表示された場合は、表示してください。あなたはそれを使用しようとする前に 'user'を初期化していますか?あなたのプログラムの上部にある外側のコードでそれを初期化してみてください。 – cdarke

+0

から 'user.getName()' コードの先頭行に割り当てられているデフォルトの名前は "ERROR" – ThePeskyWabbit

+0

[あなたのコードによって引き起こされた問題について質問するときは、人々は問題を再現するのに使うことができます。](https://stackoverflow.com/help/mcve) – gboffi

答えて

0

はあなたのコードが明示的に述べない限り、変数はローカルであると想定され、その変数への代入を行います。明示的に宣言すると、代わりにグローバルになります。

しかし、グローバルは「モジュールごと」です。

名前がインポートされると、インポートされたのコピーが、同じ名前のモジュールグローバルとして現在のモジュール内で作成されるということがよくあります。またas例:

from xxx import user 
# xxx.user has been copied to a globl named user 

user = "baz" # <--- this DOES NOT change xxx.user 

xxx.user = "ohyeah" # Now it changes xxx.user 

注意がこれについてはPythonで何か「魔法」があり、それは機能は、それらが定義されたモジュール内に記憶していることだと私は持っていると仮定します。

# module.py 

user = "No user yet" 

def setUser(newUser): 
    global user 
    user = newUser 

def getUser(): 
    return user 

# program.py 
from module import user, setUser, getUser 

print(user)  # --> "No user yet" 
print(getUser()) # --> "No user yet" 

setUser("Hey") 

print(user)  # --> "No user yet" (1) 
print(getUser()) # --> "Hey" 

user = "Foo" 
print(user)  # --> "Foo" 
print(getUser()) # --> "Hey" (2) 
はプログラムのグローバルであり、モジュールのグローバルではないため、(1)が発生します。値はインポート時にモジュールからプログラムにコピーされました。すべて同じ理由で

(2)が発生します。モジュールでコンパイルされたコードは、プログラムではなくそのモジュールであるグローバルとして認識されます。これは、プログラムで呼び出しが行われ、getUser変数(関数を含む)がここにあるにもかかわらず、これが起こります。これは、アクセスすべきグローバルである「覚えている」機能です。

コード

from xxx import yyy 

は、Python

import xxx 
yyy = xxx.yyy # simple assignment to a global 

同じであるフルプログラムに対して本当の "グローバル" を有していません。 x = 3のようなステートメントは、ローカル変数、キャプチャされた変数(nonlocal宣言)またはモジュールglobalのいずれかを常に変更します。 「真のグローバル」は単に存在しません(本当に必要な場合は、グローバル用の特定のモジュールを作成し、varの代わりにmyglobs.varを参照することができます)。

PS:コード

def addPoint(userInstance, comment): 
    global user 
    userInstance = user 
    message = "current name for user: " + userInstance.getName() 

は非常に奇妙に見えます。パラメータuserInstanceが上書きされます。あなたが意味するかもしれない

user = userInstance 

代わりに、

+0

私はこれを理解していますが、あなたの例では、 "baz"を "graz"に変更しようとしていて、 "baz"のままです。どのように私はそれを修正するだろうか? – ThePeskyWabbit

+0

つまり 'user = object1'は' user = object2'となるように変更する必要があります – ThePeskyWabbit

+0

'class.user = userData'を使って試したところ、動作しませんでした。また、 'User.user = userData'を試してみましたが、グローバルな' user' – ThePeskyWabbit

関連する問題