Unbound Method Error

L

lczancanella

Hi, i am brand new in Python, so sorry if this question is too basic,
but i have tried a lot and dont have success... I have the following
code...

class Funcoes:
def CifradorDeCesar(mensagem, chave, funcao):
mensagem_encriptada=''
if funcao == 1:
for byte in mensagem:
if byte.isalpha():
byte_encriptado=chr(ord(byte)+chave)
if byte.isupper() and ord(byte_encriptado) > 90:
byte_encriptado=chr(ord(byte_encriptado)-26)
if byte.islower() and ord(byte_encriptado) > 122:
byte_encriptado=chr(ord(byte_encriptado)-26)
else:
byte_encriptado=byte
mensagem_encriptada+=byte_encriptado
else:
for byte in mensagem:
if byte.isalpha():
byte_encriptado=chr(ord(byte)-chave)
if byte.isupper() and ord(byte_encriptado) < 65:
byte_encriptado=chr(ord(byte_encriptado)+26)
if byte.islower() and ord(byte_encriptado) < 97:
byte_encriptado=chr(ord(byte_encriptado)+26)
else:
byte_encriptado=byte
mensagem_encriptada+=byte_encriptado

return mensagem_encriptada

class MC(Funcoes, type):
def __init__(cls, clsname, bases, ns):
def addtrace(f):
def t(self, *args, **kwargs):
for att in self.__crypt__:
atribcripto = getattr(self, att)
atribdescripto = Funcoes.CifradorDeCesar
(atribcripto, 3, 2)
setattr(self, att, atribdescripto)
ret = f(self, *args, **kwargs)
for att in self.__crypt__:
atribdescripto = getattr(self, att)
atribcripto = Funcoes.CifradorDeCesar
(atribdescripto, 3, 1)
setattr(self, att, atribcripto)
# aqui seta __signature__ vazio, assina e atribui
__signature__
return ret
return t
from types import FunctionType
for name,obj in ns.items():
if type(obj) == FunctionType:
setattr(cls, name, addtrace(ns[name]))

class C():

__metaclass__ = MC
__crypt__ = ["_a", "_b"]

_a = 1
_b = 2

def add(self, a, b):
_a = a
_b = b
return a+b

then i call

and the error:

File "C:\Users\Junior\Desktop\Python\T2.py", line 37, in t
atribdescripto = Funcoes.CifradorDeCesar(atribcripto, 3, 2)
TypeError: unbound method CifradorDeCesar() must be called with
Funcoes instance as first argument (got int instance instead)
 
P

Piet van Oostrum

E> A quick look to your code suggests to me to rewrite the above lines as:
E> class Funcoes:
E> @classmethod
E> def CifradorDeCesar(self, mensagem, chave, funcao):
E> @classmethod is needed since you call:
E> 3, 2)

The method doesn't need the class at all, so a staticmethod would be
preferable:
class Funcoes:
@staticmethod
def CifradorDeCesar(self, mensagem, chave, funcao):

But as been mentioned in this thread before, there might be no reason to
use the class anyway.

The next thing you will run into is that you call CifradorDeCesar with
an int as parameter (mensagem) but it expects a string (or byte string,
at least something iterable).
 
C

Chris Rebert

You've gotten some "interesting" advice.

You want one of:

   class Funcoes:
       @staticmethod
       def CifradorDeCesar(mensagem, chave, funcao):
           ...
or:
   class Funcoes:
       def CifradorDeCesar(self, mensagem, chave, funcao):
           ...
or:
   class Funcoes:
       @class_method

There's no underscore in "classmethod" last time I checked.
       def CifradorDeCesar(class_, mensagem, chave, funcao):
           ...

You forgot one more option (the preferred one, IMHO):

#module toplevel
def CifradorDeCesar(mensagem, chave, funcao):


Cheers,
Chris
 
P

Piet van Oostrum

E> Yes, in this case self is not needed.
E> I agree but the code is not very clear about the use of this class as
E> ancestor of MC.
E> ?

I hadn't even noted this peculiarity. Maybe it was thought to be
necessary because CifradorDeCesar is used in MC. But as it is
explicitly used as Funcoes.CifradorDeCesar the Funcoes in the base
classes is useless and therefore confusing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top