分配两个函数给类?

时间:2019-04-02 08:20:25

标签: python function class

class Math:

    def __init__(self, number):
        self.number = number

    def add(self, add_num):
        return self.number + add_num

    def sub(self, sub_num):
        return self.number - sub_num

数学(5).add(5)

我得到了预期的10分

但是如果我这样做Math(5).add(5).sub(3): 我收到此错误AttributeError: 'int' object has no attribute 'sub'

4 个答案:

答案 0 :(得分:4)

为此,您的方法需要返回print([x for x in strings if substring in x.split()]) (或['Nice May', 'nice May comes'] 的新实例):

self

这里的Math现在的行为更像__iadd__

答案 1 :(得分:2)

当然。

您要做的基本上是

add

我不知道您到底要实现什么:是否要add()和sub()修改要操作的对象?在这种情况下,您可以

// Create the Claims, which will be the content of the JWT
        JwtClaims claims = new JwtClaims();
        claims.setIssuer("69a6de78-7188-47e3-e053-5b8c7c11a4d1");  // who creates the token and signs it
        claims.setAudience("appstoreconnect-v1"); // to whom the token is intended to be sent
        claims.setExpirationTimeMinutesInTheFuture(20); // time when the token will expire (10 minutes from now)
        claims.setIssuedAtToNow();
        claims.setGeneratedJwtId(); // a unique identifier for the token
     // Generate an EC key pair, which will be used for signing and verification of the JWT, wrapped in a JWK
        EllipticCurveJsonWebKey senderJwk = EcJwkGenerator.generateJwk(EllipticCurves.P256);
        // Give the JWK a Key ID (kid), which is just the polite thing to do
        senderJwk.setKeyId("-----BEGIN PRIVATE KEY-----\n" + 
                "*******************" + 

                "-----END PRIVATE KEY-----");
       // So we first create a JsonWebSignature object.
        JsonWebSignature jws = new JsonWebSignature();
        // The payload of the JWS is JSON content of the JWT Claims
        jws.setPayload(claims.toJson());
        // The JWT is signed using the sender's private key
        jws.setKey(senderJwk.getPrivateKey());
        // Set the Key ID (kid) header because it's just the polite thing to do.
        // We only have one signing key in this example but a using a Key ID helps
        // facilitate a smooth key rollover process
        jws.setKeyIdHeaderValue(senderJwk.getKeyId());
        // Set the signature algorithm on the JWT/JWS that will integrity protect the claims
        jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.ECDSA_USING_P256_CURVE_AND_SHA256);
        jws.setHeader("typ","jwt");
        // Sign the JWS and produce the compact serialization, which will be the inner JWT/JWS
        // representation, which is a string consisting of three dot ('.') separated
        // base64url-encoded parts in the form Header.Payload.Signature
        String outJwt = jws.getCompactSerialization();
        // Now you can do something with the JWT. Like send it to some other party
        // over the clouds and through the interwebs.
        System.out.println("JWT: " + outJwt);

如果您不想这样做,可以改为

a = Math(5) # a is a "Math" object
b = a.add(5) # b is what add() returns, i. e. an int
c = b.sub(3) # an int has no sub() method

在两种情况下,链接电话的预期方式均有效。

答案 2 :(得分:0)

您返回的值不是您的Math类的对象。 您必须创建一个Math对象,该对象的number属性是您的计算结果,并将其返回以使您的代码起作用。

答案 3 :(得分:0)

执行return self.number + add_num时,您返回一个整数,而不是Math类的实例。为了解决这个问题,您可以将add方法更改为 return Math(self.number + add_num)