小编Rob*_*rto的帖子

如何*不*创建实例

如果参数与预期值不匹配,我想避免创建实例.
即总之:

#!/usr/bin/env python3

class Test(object):
    def __init__(self, reallydoit = True):
        if reallydoit:
            self.done = True
        else:
            return None

make_me = Test()
make_me_not = Test(reallydoit=False)
Run Code Online (Sandbox Code Playgroud)

我想make_me_not成为None,我认为return None可以做到,但这个变量也是一个例子Test:

>>> make_me
<__main__.Test object at 0x7fd78c732390>
>>> make_me_not
<__main__.Test object at 0x7fd78c732470>
Run Code Online (Sandbox Code Playgroud)

我确定有办法做到这一点,但到目前为止,我的Google-fu让我失望了.
感谢您的任何帮助.

编辑:我宁愿这是默默处理; 条件应该被解释为"最好不要创建这个特定的实例"而不是"你正在以错误的方式使用这个类".所以是的,提出错误然后处理它是一种可能性,但我宁愿减少骚动.

python python-3.x

2
推荐指数
1
解决办法
72
查看次数

AttributeError 'NoneType' 对象没有属性 'upload_from_filename'

我在 Linux 上使用 Python 2.7.9,我在 Google-Cloud Server SDK 上遵循 Google 的示例。我的目标是将图像上传到 Google Cloud Platform,但出现以下错误。

File "/home/pi/test.py", line 15, in <module>
  zebraBlob.upload_from_filename(filename='/home/pi/Pictures/testimg.jpg')
AttributeError: 'NoneType' object has no attribute
  'upload_from_filename'
Run Code Online (Sandbox Code Playgroud)

代码:

from firebase import firebase
from google.cloud import storage
import os

firebase = firebase.FirebaseApplication('https://motion-detector-234.firebaseio.com', None)
storage_client = storage.Client.from_service_account_json('Motion Detector-8gf5445fgeeea.json')

bucket = storage_client.get_bucket('motion-detector-210fds717.appspot.com')
print ('bucket', bucket) // output: bucket, motion-detector-210717.appspot.com

zebraBlob = bucket.get_blob('testimg.jpg')
print(zebraBlob) // output: None

zebraBlob.upload_from_filename(filename='/home/pi/Pictures/testimg.jpg')
Run Code Online (Sandbox Code Playgroud)

如何解决?

python firebase google-cloud-platform

2
推荐指数
1
解决办法
3309
查看次数