Çözüldü Python Class'ta 'self' problemi

Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.

user.cs

Hectopat
Katılım
2 Kasım 2020
Mesajlar
1.923
Makaleler
14
Çözümler
33
Yer
Dünya
Herkese merhaba. Bir interpreter yazmak istiyordum ancak sınıf içindeki fonksiyon ile 'self' nesnesine ulaşamıyorum. 'self' tanımını parametre olarak görüyor ve tanım hatası veriyor. Nasıl çözebilirim?

firecode.py:
Python:
# STATICS
DIGITS = '0123456789'
VAL_INT = 'INT'
VAL_FLOAT = 'FLOAT'
OP_PLUS = 'PLUS'
OP_MINUS = 'MINUS'
OP_MUL = 'MUL'
OP_DIV = 'DIV'
OP_LPAREN = '('
OP_RPAREN = ')'

# ERROR
class Error:
    def __init__(self, name):
        self.name = name

    def get_error(self):
        return 'ERR!!! ' + self.name

# TOKEN
class Token:
    def __init__(self, _value, _type):
        self.value = _value
        self.type = _type

# LEXER
class Lexer:
    def __init__(self, text):
        self.text = text
        self.pos = 0
        self.cur_char = self.text[self.pos]

    def next(self):
        self.pos += 1
        self.cur_char = self.text[self.pos]
        if self.pos >= len(self.text): self.pos = None

    def lexing(self):
        tokens = []
        cur_str_char = ''
        dot_count = 0
        tok_type = VAL_INT
        while self.pos != None:
            if self.cur_char in DIGITS:
                cur_str_char += self.cur_char
                self.next()
            elif self.cur_char == '.':
                if dot_count == 0:
                    cur_str_char += '.'
                    dot_count += 1
                    tok_type = VAL_FLOAT
                    self.next()
                else:
                    return None, Error('Wrong float')
            elif self.cur_char == '+':
                cur_str_char = cur_str_char.strip()
                if cur_str_char.endswith('.'):
                    return None, Error("Wrong float")
                else:
                    tokens.append(Token(int(cur_str_char) if dot_count == 0 else float(cur_str_char), VAL_INT if dot_count == 0 else VAL_FLOAT))
                    tokens.append(Token('+', OP_PLUS))
                    cur_str_char = ''
                    self.next()
     
        return tokens, None

# RUN
def run(input):
    lexer = Lexer(input)
    tokens, error = Lexer.lexing()
    if error != None: print(error.get_error())
    else: print(tokens)

shell.py:
Python:
import firecode

while True:
    text = input('>> ')
    firecode.run(text)

Error:
1655989677580.png
 
Son düzenleyen: Moderatör:
Çözüm
Herkese merhaba. Bir interpreter yazmak istiyordum ancak sınıf içindeki fonksiyon ile 'self' nesnesine ulaşamıyorum. 'self' tanımını parametre olarak görüyor ve tanım hatası veriyor. Nasıl çözebilirim?

firecode.py:
Python:
# STATICS
DIGITS = '0123456789'
VAL_INT = 'INT'
VAL_FLOAT = 'FLOAT'
OP_PLUS = 'PLUS'
OP_MINUS = 'MINUS'
OP_MUL = 'MUL'
OP_DIV = 'DIV'
OP_LPAREN = '('
OP_RPAREN = ')'

# ERROR
class Error:
    def __init__(self, name):
        self.name = name

    def get_error(self):
        return 'ERR!!! ' + self.name

# TOKEN
class Token:
    def __init__(self, _value, _type):
        self.value = _value
        self.type = _type

# LEXER
class Lexer:
    def __init__(self, text):
        self.text = text
        self.pos = 0
        self.cur_char = self.text[self.pos]

    def next(self):
        self.pos += 1
        self.cur_char = self.text[self.pos]
        if self.pos >= len(self.text): self.pos = None

    def lexing(self):
        tokens = []
        cur_str_char = ''
        dot_count = 0
        tok_type = VAL_INT
        while self.pos != None:
            if self.cur_char in DIGITS:
                cur_str_char += self.cur_char
                self.next()
            elif self.cur_char == '.':
                if dot_count == 0:
                    cur_str_char += '.'
                    dot_count += 1
                    tok_type = VAL_FLOAT
                    self.next()
                else:
                    return None, Error('Wrong float')
            elif self.cur_char == '+':
                cur_str_char = cur_str_char.strip()
                if cur_str_char.endswith('.'):
                    return None, Error("Wrong float")
                else:
                    tokens.append(Token(int(cur_str_char) if dot_count == 0 else float(cur_str_char), VAL_INT if dot_count == 0 else VAL_FLOAT))
                    tokens.append(Token('+', OP_PLUS))
                    cur_str_char = ''
                    self.next()
    
        return tokens, None

# RUN
def run(input):
    lexer = Lexer(input)
    tokens, error = Lexer.lexing()
    if error != None: print(error.get_error())
    else: print(tokens)

shell.py:
Python:
import firecode

while True:
    text = input('>> ')
    firecode.run(text)

Error:
Eki Görüntüle 1428952
Lexer.lexing(lexer) yapmayi dener misin?
Herkese merhaba. Bir interpreter yazmak istiyordum ancak sınıf içindeki fonksiyon ile 'self' nesnesine ulaşamıyorum. 'self' tanımını parametre olarak görüyor ve tanım hatası veriyor. Nasıl çözebilirim?

firecode.py:
Python:
# STATICS
DIGITS = '0123456789'
VAL_INT = 'INT'
VAL_FLOAT = 'FLOAT'
OP_PLUS = 'PLUS'
OP_MINUS = 'MINUS'
OP_MUL = 'MUL'
OP_DIV = 'DIV'
OP_LPAREN = '('
OP_RPAREN = ')'

# ERROR
class Error:
    def __init__(self, name):
        self.name = name

    def get_error(self):
        return 'ERR!!! ' + self.name

# TOKEN
class Token:
    def __init__(self, _value, _type):
        self.value = _value
        self.type = _type

# LEXER
class Lexer:
    def __init__(self, text):
        self.text = text
        self.pos = 0
        self.cur_char = self.text[self.pos]

    def next(self):
        self.pos += 1
        self.cur_char = self.text[self.pos]
        if self.pos >= len(self.text): self.pos = None

    def lexing(self):
        tokens = []
        cur_str_char = ''
        dot_count = 0
        tok_type = VAL_INT
        while self.pos != None:
            if self.cur_char in DIGITS:
                cur_str_char += self.cur_char
                self.next()
            elif self.cur_char == '.':
                if dot_count == 0:
                    cur_str_char += '.'
                    dot_count += 1
                    tok_type = VAL_FLOAT
                    self.next()
                else:
                    return None, Error('Wrong float')
            elif self.cur_char == '+':
                cur_str_char = cur_str_char.strip()
                if cur_str_char.endswith('.'):
                    return None, Error("Wrong float")
                else:
                    tokens.append(Token(int(cur_str_char) if dot_count == 0 else float(cur_str_char), VAL_INT if dot_count == 0 else VAL_FLOAT))
                    tokens.append(Token('+', OP_PLUS))
                    cur_str_char = ''
                    self.next()
    
        return tokens, None

# RUN
def run(input):
    lexer = Lexer(input)
    tokens, error = Lexer.lexing()
    if error != None: print(error.get_error())
    else: print(tokens)

shell.py:
Python:
import firecode

while True:
    text = input('>> ')
    firecode.run(text)

Error:
Eki Görüntüle 1428952
Lexer.lexing(lexer) yapmayi dener misin?
 
Çözüm

Yeni konular

Geri
Yukarı